Skip to content

Commit 3fb9b88

Browse files
committed
incompatible_msrv: lint function calls with any argument count
The lint for function calls was previously restricted to functions taking exactly one argument. This was not documented. Generalizing the lint to an arbitrary number of arguments in the function call requires special casing some macro expansions from the standard library. Macros such as `panic!()` or `assert_eq!()` exist since Rust 1.0.0, but modern stdlib expand those macros into calls to functions introduced in later Rust versions. While it is desirable to lint code inside macros, using MSRV-incompatible functions coming from `core` in macro expansions has been special-cased to not trigger this lint. Also, code coming from compiler desugaring may contain function calls (for example, `a..=b` is now desugared into `RangeInclusive::new(a, b)`. Those should not be linted either as the compiler is allowed to use unstable function calls.
1 parent 0dd5c4d commit 3fb9b88

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

clippy_lints/src/incompatible_msrv.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use clippy_utils::is_in_test;
44
use clippy_utils::msrvs::Msrv;
55
use rustc_attr_parsing::{RustcVersion, StabilityLevel, StableSince};
66
use rustc_data_structures::fx::FxHashMap;
7-
use rustc_hir::{Expr, ExprKind, HirId};
7+
use rustc_hir::{Expr, ExprKind, HirId, QPath};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_session::impl_lint_pass;
1111
use rustc_span::def_id::DefId;
12-
use rustc_span::{ExpnKind, Span};
12+
use rustc_span::{ExpnKind, Span, sym};
1313

1414
declare_clippy_lint! {
1515
/// ### What it does
@@ -95,6 +95,19 @@ impl IncompatibleMsrv {
9595
// Intentionally not using `.from_expansion()`, since we do still care about macro expansions
9696
return;
9797
}
98+
// Functions coming from `core` while expanding a macro such as `assert*!()` get to cheat too: the
99+
// macros may have existed prior to the checked MSRV, but their expansion with a recent compiler
100+
// might use recent functions or methods. Compiling with an older compiler would not use those.
101+
if span.from_expansion()
102+
&& cx.tcx.crate_name(def_id.krate) == sym::core
103+
&& span
104+
.ctxt()
105+
.outer_expn_data()
106+
.macro_def_id
107+
.is_some_and(|def_id| cx.tcx.crate_name(def_id.krate) == sym::core)
108+
{
109+
return;
110+
}
98111
self.emit_lint_for(cx, span, version);
99112
}
100113

@@ -125,8 +138,11 @@ impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv {
125138
self.emit_lint_if_under_msrv(cx, method_did, expr.hir_id, span);
126139
}
127140
},
128-
ExprKind::Call(call, [_]) => {
129-
if let ExprKind::Path(qpath) = call.kind
141+
ExprKind::Call(call, _) => {
142+
// Desugaring into function calls by the compiler will use `QPath::LangItem` variants. Those should
143+
// not be linted as they will not be generated in older compilers if the function is not available,
144+
// and the compiler is allowed to call unstable functions.
145+
if let ExprKind::Path(qpath @ (QPath::Resolved(..) | QPath::TypeRelative(..))) = call.kind
130146
&& let Some(path_def_id) = cx.qpath_res(&qpath, call.hir_id).opt_def_id()
131147
{
132148
self.emit_lint_if_under_msrv(cx, path_def_id, expr.hir_id, call.span);

tests/ui/incompatible_msrv.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::incompatible_msrv)]
22
#![feature(custom_inner_attributes)]
3+
#![feature(panic_internals)]
34
#![clippy::msrv = "1.3.0"]
45

56
use std::collections::HashMap;
@@ -33,4 +34,42 @@ async fn issue12273(v: impl Future<Output = ()>) {
3334
v.await;
3435
}
3536

37+
fn core_special_treatment(p: bool) {
38+
// Do not lint code coming from `core` macros expanding into `core` function calls
39+
if p {
40+
panic!("foo"); // Do not lint
41+
}
42+
43+
// But still lint code calling `core` functions directly
44+
if p {
45+
core::panicking::panic("foo");
46+
//~^ ERROR: is `1.3.0` but this item is stable since `1.6.0`
47+
}
48+
49+
// Lint code calling `core` from non-`core` macros
50+
macro_rules! my_panic {
51+
($msg:expr) => {
52+
core::panicking::panic($msg)
53+
}; //~^ ERROR: is `1.3.0` but this item is stable since `1.6.0`
54+
}
55+
my_panic!("foo");
56+
57+
// Lint even when the macro comes from `core` and calls `core` functions
58+
assert!(core::panicking::panic("out of luck"));
59+
//~^ ERROR: is `1.3.0` but this item is stable since `1.6.0`
60+
}
61+
62+
#[clippy::msrv = "1.26.0"]
63+
fn lang_items() {
64+
// Do not lint lang items. `..=` will expand into `RangeInclusive::new()`, which was introduced
65+
// in Rust 1.27.0.
66+
let _ = 1..=3;
67+
}
68+
69+
#[clippy::msrv = "1.80.0"]
70+
fn issue14212() {
71+
let _ = std::iter::repeat_n((), 5);
72+
//~^ ERROR: is `1.80.0` but this item is stable since `1.82.0`
73+
}
74+
3675
fn main() {}

tests/ui/incompatible_msrv.stderr

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.10.0`
2-
--> tests/ui/incompatible_msrv.rs:13:39
2+
--> tests/ui/incompatible_msrv.rs:14:39
33
|
44
LL | assert_eq!(map.entry("poneyland").key(), &"poneyland");
55
| ^^^^^
@@ -8,16 +8,45 @@ LL | assert_eq!(map.entry("poneyland").key(), &"poneyland");
88
= help: to override `-D warnings` add `#[allow(clippy::incompatible_msrv)]`
99

1010
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.12.0`
11-
--> tests/ui/incompatible_msrv.rs:16:11
11+
--> tests/ui/incompatible_msrv.rs:17:11
1212
|
1313
LL | v.into_key();
1414
| ^^^^^^^^^^
1515

1616
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.4.0`
17-
--> tests/ui/incompatible_msrv.rs:20:5
17+
--> tests/ui/incompatible_msrv.rs:21:5
1818
|
1919
LL | sleep(Duration::new(1, 0));
2020
| ^^^^^
2121

22-
error: aborting due to 3 previous errors
22+
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.6.0`
23+
--> tests/ui/incompatible_msrv.rs:45:9
24+
|
25+
LL | core::panicking::panic("foo");
26+
| ^^^^^^^^^^^^^^^^^^^^^^
27+
28+
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.6.0`
29+
--> tests/ui/incompatible_msrv.rs:52:13
30+
|
31+
LL | core::panicking::panic($msg)
32+
| ^^^^^^^^^^^^^^^^^^^^^^
33+
...
34+
LL | my_panic!("foo");
35+
| ---------------- in this macro invocation
36+
|
37+
= note: this error originates in the macro `my_panic` (in Nightly builds, run with -Z macro-backtrace for more info)
38+
39+
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.6.0`
40+
--> tests/ui/incompatible_msrv.rs:58:13
41+
|
42+
LL | assert!(core::panicking::panic("out of luck"));
43+
| ^^^^^^^^^^^^^^^^^^^^^^
44+
45+
error: current MSRV (Minimum Supported Rust Version) is `1.80.0` but this item is stable since `1.82.0`
46+
--> tests/ui/incompatible_msrv.rs:71:13
47+
|
48+
LL | let _ = std::iter::repeat_n((), 5);
49+
| ^^^^^^^^^^^^^^^^^^^
50+
51+
error: aborting due to 7 previous errors
2352

0 commit comments

Comments
 (0)