Skip to content

Commit 6e1df47

Browse files
author
Evan Typanski
committed
Fix case where suggestion errored for infer type
1 parent e5ebd3e commit 6e1df47

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

clippy_lints/src/manual_rem_euclid.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::{meets_msrv, msrvs, path_to_local};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
7-
use rustc_hir::{BinOpKind, Expr, ExprKind};
7+
use rustc_hir::{BinOpKind, Expr, ExprKind, Node, TyKind};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_semver::RustcVersion;
1010
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -19,12 +19,12 @@ declare_clippy_lint! {
1919
///
2020
/// ### Example
2121
/// ```rust
22-
/// let x = 24;
22+
/// let x: i32 = 24;
2323
/// let rem = ((x % 4) + 4) % 4;
2424
/// ```
2525
/// Use instead:
2626
/// ```rust
27-
/// let x = 24;
27+
/// let x: i32 = 24;
2828
/// let rem = x.rem_euclid(4);
2929
/// ```
3030
#[clippy::version = "1.63.0"]
@@ -63,7 +63,14 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
6363
if op3.node == BinOpKind::Rem;
6464
if let Some((const3, expr3)) = check_for_positive_int_constant(cx, expr2);
6565
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);
6774
then {
6875
let mut app = Applicability::MachineApplicable;
6976
let rem_of = snippet_with_applicability(cx, expr3.span, "_", &mut app);

tests/ui/manual_rem_euclid.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ fn main() {
1414
let _: i32 = (3 + value % 4) % 4;
1515
let _: i32 = (-4 + value % -4) % -4;
1616
let _: i32 = ((5 % 4) + 4) % 4;
17+
18+
// Make sure the lint does not trigger if it would cause an error, like with an ambiguous
19+
// integer type
20+
let not_annotated = 24;
21+
let _ = ((not_annotated % 4) + 4) % 4;
22+
let inferred: _ = 24;
23+
let _ = ((inferred % 4) + 4) % 4;
1724
}

tests/ui/manual_rem_euclid.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ fn main() {
1414
let _: i32 = (3 + value % 4) % 4;
1515
let _: i32 = (-4 + value % -4) % -4;
1616
let _: i32 = ((5 % 4) + 4) % 4;
17+
18+
// Make sure the lint does not trigger if it would cause an error, like with an ambiguous
19+
// integer type
20+
let not_annotated = 24;
21+
let _ = ((not_annotated % 4) + 4) % 4;
22+
let inferred: _ = 24;
23+
let _ = ((inferred % 4) + 4) % 4;
1724
}

0 commit comments

Comments
 (0)