Skip to content

Commit b04242c

Browse files
committed
incompatible_msrv: lint function calls with any argument count
The lint for function calls was previously restricted, without documentation, to functions taking exactly one argument. However, it is necessary to special case some macro expansions from the standard library, namely `core`. Macros such as `panic!()` or `assert_eq!()` exist since Rust 1.0.0, but modern compilers use function introduced in later Rust versions in their expansion. 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 to prevent false positives.
1 parent b83762c commit b04242c

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

clippy_lints/src/incompatible_msrv.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ 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,7 +138,7 @@ 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, [_]) => {
141+
ExprKind::Call(call, _) => {
129142
if let ExprKind::Path(qpath) = call.kind
130143
&& let Some(path_def_id) = cx.qpath_res(&qpath, call.hir_id).opt_def_id()
131144
{

tests/ui/incompatible_msrv.rs

Lines changed: 17 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,20 @@ async fn issue12273(v: impl Future<Output = ()>) {
3334
v.await;
3435
}
3536

37+
fn core_special_treatment() {
38+
panic!("foo"); // Do not lint
39+
core::panicking::panic("foo");
40+
//~^ ERROR: is `1.3.0` but this item is stable since `1.6.0`
41+
}
42+
43+
#[clippy::msrv = "1.26.0"]
44+
fn lang_items() {
45+
let _ = 1..=3;
46+
}
47+
48+
#[clippy::msrv = "1.80.0"]
49+
fn issue14212() {
50+
let _ = std::iter::repeat_n((), 5);
51+
}
52+
3653
fn main() {}

tests/ui/incompatible_msrv.stderr

Lines changed: 16 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,28 @@ 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:39:5
24+
|
25+
LL | core::panicking::panic("foo");
26+
| ^^^^^^^^^^^^^^^^^^^^^^
27+
28+
error: current MSRV (Minimum Supported Rust Version) is `1.80.0` but this item is stable since `1.82.0`
29+
--> tests/ui/incompatible_msrv.rs:50:13
30+
|
31+
LL | let _ = std::iter::repeat_n((), 5);
32+
| ^^^^^^^^^^^^^^^^^^^
33+
34+
error: aborting due to 5 previous errors
2335

0 commit comments

Comments
 (0)