@@ -4,7 +4,7 @@ use clippy_utils::source::snippet_with_applicability;
4
4
use clippy_utils:: { meets_msrv, msrvs, path_to_local} ;
5
5
use if_chain:: if_chain;
6
6
use rustc_errors:: Applicability ;
7
- use rustc_hir:: { BinOpKind , Expr , ExprKind } ;
7
+ use rustc_hir:: { BinOpKind , Expr , ExprKind , Node , TyKind } ;
8
8
use rustc_lint:: { LateContext , LateLintPass } ;
9
9
use rustc_semver:: RustcVersion ;
10
10
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
@@ -19,12 +19,12 @@ declare_clippy_lint! {
19
19
///
20
20
/// ### Example
21
21
/// ```rust
22
- /// let x = 24;
22
+ /// let x: i32 = 24;
23
23
/// let rem = ((x % 4) + 4) % 4;
24
24
/// ```
25
25
/// Use instead:
26
26
/// ```rust
27
- /// let x = 24;
27
+ /// let x: i32 = 24;
28
28
/// let rem = x.rem_euclid(4);
29
29
/// ```
30
30
#[ clippy:: version = "1.63.0" ]
@@ -63,7 +63,14 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
63
63
if op3. node == BinOpKind :: Rem ;
64
64
if let Some ( ( const3, expr3) ) = check_for_positive_int_constant( cx, expr2) ;
65
65
if const1 == const2 && const2 == const3;
66
- if path_to_local( expr3) . is_some( ) ;
66
+ // Only apply if we see an explicit type annotation on the local.
67
+ if let Some ( hir_id) = path_to_local( expr3) ;
68
+ let hir = cx. tcx. hir( ) ;
69
+ if let Some ( Node :: Binding ( _) ) = hir. find( hir_id) ;
70
+ let parent = hir. get_parent_node( hir_id) ;
71
+ if let Some ( Node :: Local ( local) ) = hir. find( parent) ;
72
+ if let Some ( ty) = local. ty;
73
+ if !matches!( ty. kind, TyKind :: Infer ) ;
67
74
then {
68
75
let mut app = Applicability :: MachineApplicable ;
69
76
let rem_of = snippet_with_applicability( cx, expr3. span, "_" , & mut app) ;
0 commit comments