Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement on divide before multiply detector #156

Merged
merged 2 commits into from
Aug 14, 2023
Merged
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
76 changes: 29 additions & 47 deletions detectors/divide-before-multiply/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,68 +38,50 @@ dylint_linting::declare_late_lint! {
"Division should be performed after multiplication"
}

impl<'tcx> LateLintPass<'tcx> for DivideBeforeMultiply {
fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
_: rustc_hir::intravisit::FnKind<'tcx>,
_: &'tcx rustc_hir::FnDecl<'tcx>,
body: &'tcx rustc_hir::Body<'tcx>,
_: Span,
_: rustc_hir::HirId,
) {
struct DivideBeforeMultiplyVisitor {
has_multiplication: bool,
is_precision_loss: bool,
is_precision_loss_span: Vec<Option<Span>>,
}
fn get_divisions_inside_expr<'tcx>(expr: &'tcx Expr<'_>) -> Vec<Span> {
struct DivisionsInsideExpr {
divisions: Vec<Span>,
}

impl<'tcx> Visitor<'tcx> for DivideBeforeMultiplyVisitor {
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
if_chain! {
if let ExprKind::Binary(binop, _, _) = expr.kind;
if let BinOpKind::Mul = binop.node;
then {
self.has_multiplication = true;
walk_expr(self, expr);
}
impl<'tcx> Visitor<'tcx> for DivisionsInsideExpr {
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
if_chain! {
if let ExprKind::Binary(op, _lexpr, _rexpr) = expr.kind;
if BinOpKind::Div == op.node;
then{
self.divisions.push(expr.span);
}

if_chain!(
if self.has_multiplication;
if let ExprKind::Binary(binop, _, _) = expr.kind;
if let BinOpKind::Div = binop.node;
then {
self.is_precision_loss = true;
self.is_precision_loss_span.push(Some(expr.span));
}
);

walk_expr(self, expr);
}
walk_expr(self, expr);
}
}

let mut visitor = DivisionsInsideExpr {
divisions: Vec::default(),
};

let mut visitor = DivideBeforeMultiplyVisitor {
has_multiplication: false,
is_precision_loss: false,
is_precision_loss_span: Vec::new(),
};
walk_expr(&mut visitor, expr);

walk_expr(&mut visitor, body.value);
return visitor.divisions;
}

if visitor.is_precision_loss {
visitor.is_precision_loss_span.iter().for_each(|span| {
if let Some(span) = span {
impl<'tcx> LateLintPass<'tcx> for DivideBeforeMultiply {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
if_chain! {
if let ExprKind::Binary(op, _lexpr, _rexpr) = expr.kind;
if BinOpKind::Mul == op.node;
then{
for division in get_divisions_inside_expr(expr) {
span_lint_and_help(
cx,
DIVIDE_BEFORE_MULTIPLY,
*span,
division,
"Division before multiplication might result in a loss of precision",
None,
"Consider reversing the order of operations to reduce the loss of precision.",
);
}
});
}
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions detectors/unprotected-mapping-operation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ extern crate rustc_hir;
extern crate rustc_middle;
extern crate rustc_span;

use clippy_utils::diagnostics::span_lint;
use std::collections::HashSet;

use clippy_utils::diagnostics::span_lint;
use rustc_hir::QPath;
use rustc_hir::{
intravisit::{walk_expr, Visitor},
Expand Down Expand Up @@ -148,7 +149,7 @@ impl<'tcx> LateLintPass<'tcx> for UnprotectedMappingOperation {
&caller_and_map_ops,
false,
&mut vec![],
&mut HashSet::<BasicBlock>::default()
&mut HashSet::<BasicBlock>::default(),
);
for place in unchecked_places {
span_lint(
Expand Down Expand Up @@ -229,7 +230,7 @@ impl<'tcx> LateLintPass<'tcx> for UnprotectedMappingOperation {
caller_and_map_ops,
comparison_with_caller,
tainted_places,
visited_bbs
visited_bbs,
));
}
return ret_vec;
Expand Down Expand Up @@ -277,7 +278,7 @@ impl<'tcx> LateLintPass<'tcx> for UnprotectedMappingOperation {
caller_and_map_ops,
after_comparison,
tainted_places,
visited_bbs
visited_bbs,
));
}
}
Expand All @@ -290,7 +291,7 @@ impl<'tcx> LateLintPass<'tcx> for UnprotectedMappingOperation {
caller_and_map_ops,
after_comparison,
tainted_places,
visited_bbs
visited_bbs,
));
}
TerminatorKind::Yield { resume, .. } => {
Expand All @@ -300,7 +301,7 @@ impl<'tcx> LateLintPass<'tcx> for UnprotectedMappingOperation {
caller_and_map_ops,
after_comparison,
tainted_places,
visited_bbs
visited_bbs,
));
}
TerminatorKind::FalseEdge { real_target, .. }
Expand All @@ -311,7 +312,7 @@ impl<'tcx> LateLintPass<'tcx> for UnprotectedMappingOperation {
caller_and_map_ops,
after_comparison,
tainted_places,
visited_bbs
visited_bbs,
));
}
TerminatorKind::InlineAsm { destination, .. } => {
Expand All @@ -322,7 +323,7 @@ impl<'tcx> LateLintPass<'tcx> for UnprotectedMappingOperation {
caller_and_map_ops,
after_comparison,
tainted_places,
visited_bbs
visited_bbs,
));
}
}
Expand Down