Add external macro guard and test middle MSRV

This commit is contained in:
Evan Typanski 2022-06-23 16:34:52 -04:00
parent 92704b494a
commit df26c3f551
6 changed files with 76 additions and 9 deletions

View file

@ -4,7 +4,8 @@ use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{in_constant, meets_msrv, msrvs, path_to_local};
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind, Node, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_semver::RustcVersion;
use rustc_session::{declare_tool_lint, impl_lint_pass};
@ -55,6 +56,10 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
return;
}
if in_external_macro(cx.sess(), expr.span) {
return;
}
if let ExprKind::Binary(op1, expr1, right) = expr.kind
&& op1.node == BinOpKind::Rem
&& let Some(const1) = check_for_unsigned_int_constant(cx, right)

View file

@ -127,3 +127,11 @@ macro_rules! ptr_as_ptr_cast {
$ptr as *const i32
};
}
#[macro_export]
macro_rules! manual_rem_euclid {
() => {
let value: i32 = 5;
let _: i32 = ((value % 4) + 4) % 4;
};
}

View file

@ -1,7 +1,18 @@
// run-rustfix
// aux-build:macro_rules.rs
#![warn(clippy::manual_rem_euclid)]
#[macro_use]
extern crate macro_rules;
macro_rules! internal_rem_euclid {
() => {
let value: i32 = 5;
let _: i32 = value.rem_euclid(4);
};
}
fn main() {
let value: i32 = 5;
@ -25,6 +36,12 @@ fn main() {
// For lint to apply the constant must always be on the RHS of the previous value for %
let _: i32 = 4 % ((value % 4) + 4);
let _: i32 = ((4 % value) + 4) % 4;
// Lint in internal macros
internal_rem_euclid!();
// Do not lint in external macros
manual_rem_euclid!();
}
// Should lint for params too

View file

@ -1,7 +1,18 @@
// run-rustfix
// aux-build:macro_rules.rs
#![warn(clippy::manual_rem_euclid)]
#[macro_use]
extern crate macro_rules;
macro_rules! internal_rem_euclid {
() => {
let value: i32 = 5;
let _: i32 = ((value % 4) + 4) % 4;
};
}
fn main() {
let value: i32 = 5;
@ -25,6 +36,12 @@ fn main() {
// For lint to apply the constant must always be on the RHS of the previous value for %
let _: i32 = 4 % ((value % 4) + 4);
let _: i32 = ((4 % value) + 4) % 4;
// Lint in internal macros
internal_rem_euclid!();
// Do not lint in external macros
manual_rem_euclid!();
}
// Should lint for params too

View file

@ -1,5 +1,5 @@
error: manual `rem_euclid` implementation
--> $DIR/manual_rem_euclid.rs:8:18
--> $DIR/manual_rem_euclid.rs:19:18
|
LL | let _: i32 = ((value % 4) + 4) % 4;
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)`
@ -7,40 +7,51 @@ LL | let _: i32 = ((value % 4) + 4) % 4;
= note: `-D clippy::manual-rem-euclid` implied by `-D warnings`
error: manual `rem_euclid` implementation
--> $DIR/manual_rem_euclid.rs:9:18
--> $DIR/manual_rem_euclid.rs:20:18
|
LL | let _: i32 = (4 + (value % 4)) % 4;
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)`
error: manual `rem_euclid` implementation
--> $DIR/manual_rem_euclid.rs:10:18
--> $DIR/manual_rem_euclid.rs:21:18
|
LL | let _: i32 = (value % 4 + 4) % 4;
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)`
error: manual `rem_euclid` implementation
--> $DIR/manual_rem_euclid.rs:11:18
--> $DIR/manual_rem_euclid.rs:22:18
|
LL | let _: i32 = (4 + value % 4) % 4;
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)`
error: manual `rem_euclid` implementation
--> $DIR/manual_rem_euclid.rs:12:22
--> $DIR/manual_rem_euclid.rs:23:22
|
LL | let _: i32 = 1 + (4 + value % 4) % 4;
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)`
error: manual `rem_euclid` implementation
--> $DIR/manual_rem_euclid.rs:32:5
--> $DIR/manual_rem_euclid.rs:12:22
|
LL | let _: i32 = ((value % 4) + 4) % 4;
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)`
...
LL | internal_rem_euclid!();
| ---------------------- in this macro invocation
|
= note: this error originates in the macro `internal_rem_euclid` (in Nightly builds, run with -Z macro-backtrace for more info)
error: manual `rem_euclid` implementation
--> $DIR/manual_rem_euclid.rs:49:5
|
LL | ((num % 4) + 4) % 4
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `num.rem_euclid(4)`
error: manual `rem_euclid` implementation
--> $DIR/manual_rem_euclid.rs:37:5
--> $DIR/manual_rem_euclid.rs:54:5
|
LL | ((num % 4) + 4) % 4
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `num.rem_euclid(4)`
error: aborting due to 7 previous errors
error: aborting due to 8 previous errors

View file

@ -217,3 +217,12 @@ mod just_above_msrv {
}
}
}
mod const_rem_euclid {
#![feature(custom_inner_attributes)]
#![clippy::msrv = "1.50.0"]
pub const fn const_rem_euclid_4(num: i32) -> i32 {
((num % 4) + 4) % 4
}
}