Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::clippy_utils::source::HasSession;
use clippy_utils::diagnostics::span_lint;
use rustc_ast::BinOpKind;
use rustc_hir::Expr;
Expand All @@ -13,11 +14,12 @@ pub(super) fn check(cx: &LateContext<'_>, op: BinOpKind, lhs: &Expr<'_>, rhs: &E
&& let rhs_ty = cx.typeck_results().expr_ty(rhs)
&& let ty::Int(_) | ty::Uint(_) = lhs_ty.peel_refs().kind()
&& let ty::Int(_) | ty::Uint(_) = rhs_ty.peel_refs().kind()
&& !span.in_external_macro(cx.sess().source_map())
{
span_lint(
cx,
INTEGER_DIVISION_REMAINDER_USED,
span.source_callsite(),
span,
format!("use of `{}` has been disallowed in this context", op.as_str()),
);
}
Expand Down
16 changes: 15 additions & 1 deletion tests/ui/integer_division_remainder_used.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@aux-build: proc_macros.rs
#![warn(clippy::integer_division_remainder_used)]
#![allow(unused_variables)]
#![allow(clippy::op_ref)]

struct CustomOps(pub i32);
Expand Down Expand Up @@ -47,4 +47,18 @@ fn main() {
let w = CustomOps(3);
let x = CustomOps(4);
let z = w % x;

macro_rules! mac {
($a:expr, $b:expr) => {
a % b
//~^ integer_division_remainder_used
};
}
// should not trigger if from expansion in external macro
let issue17048 = mac!(a, b);
let issue17048 = proc_macros::external! {{
let a = 10;
let b = 5;
a % b
}};
}
13 changes: 12 additions & 1 deletion tests/ui/integer_division_remainder_used.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,16 @@ error: use of `/` has been disallowed in this context
LL | let i = a / &4;
| ^^^^^^

error: aborting due to 9 previous errors
error: use of `%` has been disallowed in this context
--> tests/ui/integer_division_remainder_used.rs:53:13
|
LL | a % b
| ^^^^^
...
LL | let issue17048 = mac!(a, b);
| ---------- in this macro invocation
|
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 10 previous errors