Skip to content

Commit 18061e2

Browse files
authored
incompatible_msrv: lint function calls with any argument count (rust-lang#14216)
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. Fix rust-lang#14212 changelog: [`incompatible_msrv`]: lint function calls with any argument count
2 parents 7254072 + 4f0e507 commit 18061e2

File tree

3 files changed

+94
-8
lines changed

3 files changed

+94
-8
lines changed

clippy_lints/src/incompatible_msrv.rs

Lines changed: 22 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
@@ -93,6 +93,21 @@ impl IncompatibleMsrv {
9393
// Intentionally not using `.from_expansion()`, since we do still care about macro expansions
9494
return;
9595
}
96+
97+
// Functions coming from `core` while expanding a macro such as `assert*!()` get to cheat too: the
98+
// macros may have existed prior to the checked MSRV, but their expansion with a recent compiler
99+
// might use recent functions or methods. Compiling with an older compiler would not use those.
100+
if span.from_expansion()
101+
&& cx.tcx.crate_name(def_id.krate) == sym::core
102+
&& span
103+
.ctxt()
104+
.outer_expn_data()
105+
.macro_def_id
106+
.is_some_and(|def_id| cx.tcx.crate_name(def_id.krate) == sym::core)
107+
{
108+
return;
109+
}
110+
96111
if (self.check_in_tests || !is_in_test(cx.tcx, node))
97112
&& let Some(current) = self.msrv.current(cx)
98113
&& let version = self.get_def_id_version(cx.tcx, def_id)
@@ -118,8 +133,11 @@ impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv {
118133
self.emit_lint_if_under_msrv(cx, method_did, expr.hir_id, span);
119134
}
120135
},
121-
ExprKind::Call(call, [_]) => {
122-
if let ExprKind::Path(qpath) = call.kind
136+
ExprKind::Call(call, _) => {
137+
// Desugaring into function calls by the compiler will use `QPath::LangItem` variants. Those should
138+
// not be linted as they will not be generated in older compilers if the function is not available,
139+
// and the compiler is allowed to call unstable functions.
140+
if let ExprKind::Path(qpath @ (QPath::Resolved(..) | QPath::TypeRelative(..))) = call.kind
123141
&& let Some(path_def_id) = cx.qpath_res(&qpath, call.hir_id).opt_def_id()
124142
{
125143
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;
@@ -34,4 +35,42 @@ async fn issue12273(v: impl Future<Output = ()>) {
3435
v.await;
3536
}
3637

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