Skip to content

[arithmetic-side-effects] Do not ignore literal references #9587

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

Merged
merged 2 commits into from
Oct 4, 2022
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
50 changes: 30 additions & 20 deletions clippy_lints/src/operators/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#![allow(
// False positive
clippy::match_same_arms
)]

use super::ARITHMETIC_SIDE_EFFECTS;
use clippy_utils::{consts::constant_simple, diagnostics::span_lint};
use rustc_ast as ast;
Expand All @@ -14,12 +9,12 @@ use rustc_session::impl_lint_pass;
use rustc_span::source_map::{Span, Spanned};

const HARD_CODED_ALLOWED: &[&str] = &[
"&str",
"f32",
"f64",
"std::num::Saturating",
"std::num::Wrapping",
"std::string::String",
"&str",
];

#[derive(Debug)]
Expand Down Expand Up @@ -50,10 +45,10 @@ impl ArithmeticSideEffects {
let ast::LitKind::Int(value, _) = lit.node
{
match (&op.node, value) {
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0) |
(hir::BinOpKind::Mul, 0 | 1) => true,
(hir::BinOpKind::Div | hir::BinOpKind::Rem, 0) => false,
(hir::BinOpKind::Div | hir::BinOpKind::Rem, _) => true,
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
| (hir::BinOpKind::Div | hir::BinOpKind::Rem, _)
| (hir::BinOpKind::Mul, 0 | 1) => true,
_ => false,
}
} else {
Expand All @@ -79,16 +74,13 @@ impl ArithmeticSideEffects {
self.expr_span = Some(expr.span);
}

/// * If `expr` is a literal integer like `1` or `i32::MAX`, returns itself.
/// * Is `expr` is a literal integer reference like `&199`, returns the literal integer without
/// references.
/// * If `expr` is anything else, returns `None`.
fn literal_integer<'expr, 'tcx>(expr: &'expr hir::Expr<'tcx>) -> Option<&'expr hir::Expr<'tcx>> {
/// If `expr` does not match any variant of `LiteralIntegerTy`, returns `None`.
fn literal_integer<'expr, 'tcx>(expr: &'expr hir::Expr<'tcx>) -> Option<LiteralIntegerTy<'expr, 'tcx>> {
if matches!(expr.kind, hir::ExprKind::Lit(_)) {
return Some(expr);
return Some(LiteralIntegerTy::Value(expr));
}
if let hir::ExprKind::AddrOf(.., inn) = expr.kind && let hir::ExprKind::Lit(_) = inn.kind {
return Some(inn)
return Some(LiteralIntegerTy::Ref(inn));
}
None
}
Expand Down Expand Up @@ -126,10 +118,9 @@ impl ArithmeticSideEffects {
}
let has_valid_op = if Self::is_integral(lhs_ty) && Self::is_integral(rhs_ty) {
match (Self::literal_integer(lhs), Self::literal_integer(rhs)) {
(None, None) => false,
(None, Some(local_expr)) => Self::has_valid_op(op, local_expr),
(Some(local_expr), None) => Self::has_valid_op(op, local_expr),
(Some(_), Some(_)) => true,
(None, Some(lit_int_ty)) | (Some(lit_int_ty), None) => Self::has_valid_op(op, lit_int_ty.into()),
(Some(LiteralIntegerTy::Value(_)), Some(LiteralIntegerTy::Value(_))) => true,
(None, None) | (Some(_), Some(_)) => false,
}
} else {
false
Expand Down Expand Up @@ -186,3 +177,22 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects {
}
}
}

/// Tells if an expression is a integer declared by value or by reference.
///
/// If `LiteralIntegerTy::Ref`, then the contained value will be `hir::ExprKind::Lit` rather
/// than `hirExprKind::Addr`.
enum LiteralIntegerTy<'expr, 'tcx> {
/// For example, `&199`
Ref(&'expr hir::Expr<'tcx>),
/// For example, `1` or `i32::MAX`
Value(&'expr hir::Expr<'tcx>),
}

impl<'expr, 'tcx> From<LiteralIntegerTy<'expr, 'tcx>> for &'expr hir::Expr<'tcx> {
fn from(from: LiteralIntegerTy<'expr, 'tcx>) -> Self {
match from {
LiteralIntegerTy::Ref(elem) | LiteralIntegerTy::Value(elem) => elem,
}
}
}
4 changes: 3 additions & 1 deletion tests/ui/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ pub fn non_overflowing_ops_or_ops_already_handled_by_the_compiler_should_not_tri
_n = 1 * _n;
_n = &1 * _n;
_n = 23 + 85;
_n = &23 + &85;

// Unary
_n = -1;
Expand Down Expand Up @@ -187,6 +186,9 @@ pub fn runtime_ops() {
_n = _n * &2;
_n = 2 * _n;
_n = &2 * _n;
_n = 23 + &85;
_n = &23 + 85;
_n = &23 + &85;

// Custom
let _ = Custom + 0;
Expand Down
Loading