Skip to content

Commit 91c43a4

Browse files
committed
Consider referenced allowed or hard-coded types
1 parent 1407c76 commit 91c43a4

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const HARD_CODED_ALLOWED_BINARY: &[[&str; 2]] = &[
2121
["f64", "f64"],
2222
["std::num::Saturating", "std::num::Saturating"],
2323
["std::num::Wrapping", "std::num::Wrapping"],
24-
["std::string::String", "&str"],
24+
["std::string::String", "str"],
2525
];
2626
const HARD_CODED_ALLOWED_UNARY: &[&str] = &["f32", "f64", "std::num::Saturating", "std::num::Wrapping"];
2727
const INTEGER_METHODS: &[&str] = &["saturating_div", "wrapping_div", "wrapping_rem", "wrapping_rem_euclid"];
@@ -67,9 +67,17 @@ impl ArithmeticSideEffects {
6767
/// "multiplication" are present in the inner set of allowed types.
6868
fn has_allowed_binary(&self, lhs_ty: Ty<'_>, rhs_ty: Ty<'_>) -> bool {
6969
let lhs_ty_string = lhs_ty.to_string();
70-
let lhs_ty_string_elem = lhs_ty_string.split('<').next().unwrap_or_default();
70+
let lhs_ty_string_elem = lhs_ty_string
71+
.split('<')
72+
.next()
73+
.unwrap_or_default()
74+
.trim_start_matches('&');
7175
let rhs_ty_string = rhs_ty.to_string();
72-
let rhs_ty_string_elem = rhs_ty_string.split('<').next().unwrap_or_default();
76+
let rhs_ty_string_elem = rhs_ty_string
77+
.split('<')
78+
.next()
79+
.unwrap_or_default()
80+
.trim_start_matches('&');
7381
if let Some(rhs_from_specific) = self.allowed_binary.get(lhs_ty_string_elem)
7482
&& {
7583
let rhs_has_allowed_ty = rhs_from_specific.contains(rhs_ty_string_elem);
@@ -144,8 +152,10 @@ impl ArithmeticSideEffects {
144152
) {
145153
return;
146154
};
147-
let lhs_ty = cx.typeck_results().expr_ty(lhs);
148-
let rhs_ty = cx.typeck_results().expr_ty(rhs);
155+
let (actual_lhs, lhs_ref_counter) = peel_hir_expr_refs(lhs);
156+
let (actual_rhs, rhs_ref_counter) = peel_hir_expr_refs(rhs);
157+
let lhs_ty = cx.typeck_results().expr_ty(actual_lhs);
158+
let rhs_ty = cx.typeck_results().expr_ty(actual_rhs);
149159
if self.has_allowed_binary(lhs_ty, rhs_ty) {
150160
return;
151161
}
@@ -154,8 +164,6 @@ impl ArithmeticSideEffects {
154164
// At least for integers, shifts are already handled by the CTFE
155165
return;
156166
}
157-
let (actual_lhs, lhs_ref_counter) = peel_hir_expr_refs(lhs);
158-
let (actual_rhs, rhs_ref_counter) = peel_hir_expr_refs(rhs);
159167
match (
160168
Self::literal_integer(cx, actual_lhs),
161169
Self::literal_integer(cx, actual_rhs),

tests/ui/arithmetic_side_effects.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,12 @@ pub fn issue_10583(a: u16) -> u16 {
458458
10 / a
459459
}
460460

461+
pub fn issue_10767() {
462+
let n = &1.0;
463+
n + n;
464+
3.1_f32 + &1.2_f32;
465+
&3.4_f32 + 1.5_f32;
466+
&3.5_f32 + &1.3_f32;
467+
}
468+
461469
fn main() {}

0 commit comments

Comments
 (0)