Add MSRV check for const rem_euclid

This commit is contained in:
Evan Typanski 2022-06-22 14:17:52 -04:00
parent 90f8277fe3
commit 61e1870aff
5 changed files with 23 additions and 3 deletions

View file

@ -1,7 +1,7 @@
use clippy_utils::consts::{constant_full_int, FullInt};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{meets_msrv, msrvs, path_to_local};
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};
@ -51,6 +51,10 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
return;
}
if in_constant(cx, expr.hir_id) && !meets_msrv(self.msrv, msrvs::REM_EUCLID_CONST) {
return;
}
if let ExprKind::Binary(op1, ..) = expr.kind
&& op1.node == BinOpKind::Rem
&& let Some((const1, expr1)) = check_for_positive_int_constant(cx, expr, false)

View file

@ -13,7 +13,7 @@ macro_rules! msrv_aliases {
// names may refer to stabilized feature flags or library items
msrv_aliases! {
1,53,0 { OR_PATTERNS, MANUAL_BITS }
1,52,0 { STR_SPLIT_ONCE }
1,52,0 { STR_SPLIT_ONCE, REM_EUCLID_CONST }
1,51,0 { BORROW_AS_PTR, UNSIGNED_ABS }
1,50,0 { BOOL_THEN }
1,47,0 { TAU }

View file

@ -31,3 +31,8 @@ fn main() {
pub fn rem_euclid_4(num: i32) -> i32 {
num.rem_euclid(4)
}
// Constant version came later, should still lint
pub const fn const_rem_euclid_4(num: i32) -> i32 {
num.rem_euclid(4)
}

View file

@ -31,3 +31,8 @@ fn main() {
pub fn rem_euclid_4(num: i32) -> i32 {
((num % 4) + 4) % 4
}
// Constant version came later, should still lint
pub const fn const_rem_euclid_4(num: i32) -> i32 {
((num % 4) + 4) % 4
}

View file

@ -36,5 +36,11 @@ error: manual `rem_euclid` implementation
LL | ((num % 4) + 4) % 4
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `num.rem_euclid(4)`
error: aborting due to 6 previous errors
error: manual `rem_euclid` implementation
--> $DIR/manual_rem_euclid.rs:37:5
|
LL | ((num % 4) + 4) % 4
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `num.rem_euclid(4)`
error: aborting due to 7 previous errors