Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a8df16a

Browse files
committed
Move IntegerDivision into Operators lint pass
1 parent 83de67c commit a8df16a

File tree

6 files changed

+58
-65
lines changed

6 files changed

+58
-65
lines changed

clippy_lints/src/integer_division.rs

Lines changed: 0 additions & 61 deletions
This file was deleted.

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ store.register_lints(&[
194194
init_numbered_fields::INIT_NUMBERED_FIELDS,
195195
inline_fn_without_body::INLINE_FN_WITHOUT_BODY,
196196
int_plus_one::INT_PLUS_ONE,
197-
integer_division::INTEGER_DIVISION,
198197
invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS,
199198
items_after_statements::ITEMS_AFTER_STATEMENTS,
200199
iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR,
@@ -434,6 +433,7 @@ store.register_lints(&[
434433
operators::IDENTITY_OP,
435434
operators::INEFFECTIVE_BIT_MASK,
436435
operators::INTEGER_ARITHMETIC,
436+
operators::INTEGER_DIVISION,
437437
operators::MISREFACTORED_ASSIGN_OP,
438438
operators::OP_REF,
439439
operators::VERBOSE_BIT_MASK,

clippy_lints/src/lib.register_restriction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
2525
LintId::of(implicit_return::IMPLICIT_RETURN),
2626
LintId::of(indexing_slicing::INDEXING_SLICING),
2727
LintId::of(inherent_impl::MULTIPLE_INHERENT_IMPL),
28-
LintId::of(integer_division::INTEGER_DIVISION),
2928
LintId::of(large_include_file::LARGE_INCLUDE_FILE),
3029
LintId::of(let_underscore::LET_UNDERSCORE_MUST_USE),
3130
LintId::of(literal_representation::DECIMAL_LITERAL_REPRESENTATION),
@@ -52,6 +51,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
5251
LintId::of(modulo_arithmetic::MODULO_ARITHMETIC),
5352
LintId::of(operators::FLOAT_ARITHMETIC),
5453
LintId::of(operators::INTEGER_ARITHMETIC),
54+
LintId::of(operators::INTEGER_DIVISION),
5555
LintId::of(panic_in_result_fn::PANIC_IN_RESULT_FN),
5656
LintId::of(panic_unimplemented::PANIC),
5757
LintId::of(panic_unimplemented::TODO),

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ mod inherent_to_string;
254254
mod init_numbered_fields;
255255
mod inline_fn_without_body;
256256
mod int_plus_one;
257-
mod integer_division;
258257
mod invalid_upcast_comparisons;
259258
mod items_after_statements;
260259
mod iter_not_returning_iterator;
@@ -734,7 +733,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
734733
store.register_late_pass(|| Box::new(assertions_on_constants::AssertionsOnConstants));
735734
store.register_late_pass(|| Box::new(transmuting_null::TransmutingNull));
736735
store.register_late_pass(|| Box::new(path_buf_push_overwrite::PathBufPushOverwrite));
737-
store.register_late_pass(|| Box::new(integer_division::IntegerDivision));
738736
store.register_late_pass(|| Box::new(inherent_to_string::InherentToString));
739737
let max_trait_bounds = conf.max_trait_bounds;
740738
store.register_late_pass(move || Box::new(trait_bounds::TraitBounds::new(max_trait_bounds)));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_hir as hir;
3+
use rustc_lint::LateContext;
4+
5+
use super::INTEGER_DIVISION;
6+
7+
pub(crate) fn check<'tcx>(
8+
cx: &LateContext<'tcx>,
9+
expr: &'tcx hir::Expr<'_>,
10+
op: hir::BinOpKind,
11+
left: &'tcx hir::Expr<'_>,
12+
right: &'tcx hir::Expr<'_>,
13+
) {
14+
if op == hir::BinOpKind::Div
15+
&& cx.typeck_results().expr_ty(left).is_integral()
16+
&& cx.typeck_results().expr_ty(right).is_integral()
17+
{
18+
span_lint_and_help(
19+
cx,
20+
INTEGER_DIVISION,
21+
expr.span,
22+
"integer division",
23+
None,
24+
"division of integers may cause loss of precision. consider using floats",
25+
);
26+
}
27+
}

clippy_lints/src/operators/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod eq_op;
1111
mod erasing_op;
1212
mod float_equality_without_abs;
1313
mod identity_op;
14+
mod integer_division;
1415
mod misrefactored_assign_op;
1516
mod numeric_arithmetic;
1617
mod op_ref;
@@ -437,6 +438,32 @@ declare_clippy_lint! {
437438
"using identity operations, e.g., `x + 0` or `y / 1`"
438439
}
439440

441+
declare_clippy_lint! {
442+
/// ### What it does
443+
/// Checks for division of integers
444+
///
445+
/// ### Why is this bad?
446+
/// When outside of some very specific algorithms,
447+
/// integer division is very often a mistake because it discards the
448+
/// remainder.
449+
///
450+
/// ### Example
451+
/// ```rust
452+
/// let x = 3 / 2;
453+
/// println!("{}", x);
454+
/// ```
455+
///
456+
/// Use instead:
457+
/// ```rust
458+
/// let x = 3f32 / 2f32;
459+
/// println!("{}", x);
460+
/// ```
461+
#[clippy::version = "1.37.0"]
462+
pub INTEGER_DIVISION,
463+
restriction,
464+
"integer division may cause loss of precision"
465+
}
466+
440467
pub struct Operators {
441468
arithmetic_context: numeric_arithmetic::Context,
442469
verbose_bit_mask_threshold: u64,
@@ -457,6 +484,7 @@ impl_lint_pass!(Operators => [
457484
ERASING_OP,
458485
FLOAT_EQUALITY_WITHOUT_ABS,
459486
IDENTITY_OP,
487+
INTEGER_DIVISION,
460488
]);
461489
impl Operators {
462490
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -486,6 +514,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
486514
double_comparison::check(cx, op.node, lhs, rhs, e.span);
487515
duration_subsec::check(cx, e, op.node, lhs, rhs);
488516
float_equality_without_abs::check(cx, e, op.node, lhs, rhs);
517+
integer_division::check(cx, e, op.node, lhs, rhs);
489518
},
490519
ExprKind::AssignOp(op, lhs, rhs) => {
491520
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);

0 commit comments

Comments
 (0)