-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #4195 - thiagoarrais:division-of-integer-literals, r=fl…
…ip1995 Adds lint for integer division Hi, folks! This is my first contribution to clippy and my first real piece of Rust code. This is supposed to add a lint that warns for division of integers (#109). Please let me know if you need any changes. Fixes #109 changelog: Add lint for integer division
- Loading branch information
Showing
8 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::utils::span_help_and_lint; | ||
use if_chain::if_chain; | ||
use rustc::hir; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for division of integers | ||
/// | ||
/// **Why is this bad?** When outside of some very specific algorithms, | ||
/// integer division is very often a mistake because it discards the | ||
/// remainder. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// fn main() { | ||
/// let x = 3 / 2; | ||
/// println!("{}", x); | ||
/// } | ||
/// ``` | ||
pub INTEGER_DIVISION, | ||
pedantic, | ||
"integer division may cause loss of precision" | ||
} | ||
|
||
declare_lint_pass!(IntegerDivision => [INTEGER_DIVISION]); | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IntegerDivision { | ||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) { | ||
if is_integer_division(cx, expr) { | ||
span_help_and_lint( | ||
cx, | ||
INTEGER_DIVISION, | ||
expr.span, | ||
"integer division", | ||
"division of integers may cause loss of precision. consider using floats.", | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn is_integer_division<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) -> bool { | ||
if_chain! { | ||
if let hir::ExprKind::Binary(binop, left, right) = &expr.node; | ||
if let hir::BinOpKind::Div = &binop.node; | ||
then { | ||
let (left_ty, right_ty) = (cx.tables.expr_ty(left), cx.tables.expr_ty(right)); | ||
return left_ty.is_integral() && right_ty.is_integral(); | ||
} | ||
} | ||
|
||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#![warn(clippy::integer_division)] | ||
|
||
fn main() { | ||
let two = 2; | ||
let n = 1 / 2; | ||
let o = 1 / two; | ||
let p = two / 4; | ||
let x = 1. / 2.0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error: integer division | ||
--> $DIR/integer_division.rs:5:13 | ||
| | ||
LL | let n = 1 / 2; | ||
| ^^^^^ | ||
| | ||
= note: `-D clippy::integer-division` implied by `-D warnings` | ||
= help: division of integers may cause loss of precision. consider using floats. | ||
|
||
error: integer division | ||
--> $DIR/integer_division.rs:6:13 | ||
| | ||
LL | let o = 1 / two; | ||
| ^^^^^^^ | ||
| | ||
= help: division of integers may cause loss of precision. consider using floats. | ||
|
||
error: integer division | ||
--> $DIR/integer_division.rs:7:13 | ||
| | ||
LL | let p = two / 4; | ||
| ^^^^^^^ | ||
| | ||
= help: division of integers may cause loss of precision. consider using floats. | ||
|
||
error: aborting due to 3 previous errors | ||
|