Skip to content

Commit ee6d5c5

Browse files
committed
contants peel_refs to catch x << &0
1 parent bc0579f commit ee6d5c5

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

clippy_lints/src/identity_op.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ fn is_allowed(cx: &LateContext<'_>, cmp: BinOp, left: &Expr<'_>, right: &Expr<'_
7171
}
7272

7373
fn check(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span) {
74-
if let Some(Constant::Int(v)) = constant_simple(cx, cx.typeck_results(), e) {
75-
let check = match *cx.typeck_results().expr_ty(e).kind() {
74+
if let Some(Constant::Int(v)) = constant_simple(cx, cx.typeck_results(), e).map(Constant::peel_refs) {
75+
let check = match *cx.typeck_results().expr_ty(e).peel_refs().kind() {
7676
ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
7777
ty::Uint(uty) => clip(cx.tcx, !0, uty),
7878
_ => return,

clippy_utils/src/consts.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ impl Constant {
168168
None
169169
}
170170
}
171+
172+
#[must_use]
173+
pub fn peel_refs(mut self) -> Self {
174+
while let Constant::Ref(r) = self {
175+
self = *r;
176+
}
177+
self
178+
}
171179
}
172180

173181
/// Parses a `LitKind` to a `Constant`.

tests/ui/identity_op.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ impl std::ops::Shl<i32> for A {
1111
self
1212
}
1313
}
14-
1514
#[allow(
1615
clippy::eq_op,
1716
clippy::no_effect,
1817
clippy::unnecessary_operation,
18+
clippy::op_ref,
1919
clippy::double_parens
2020
)]
2121
#[warn(clippy::identity_op)]
@@ -49,6 +49,7 @@ fn main() {
4949
1 >> 0;
5050
42 >> 0;
5151
&x >> 0;
52+
x >> &0;
5253

5354
let mut a = A("".into());
5455
let b = a << 0; // no error: non-integer

tests/ui/identity_op.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,11 @@ error: the operation is ineffective. Consider reducing it to `&x`
7272
LL | &x >> 0;
7373
| ^^^^^^^
7474

75-
error: aborting due to 12 previous errors
75+
error: the operation is ineffective. Consider reducing it to `x`
76+
--> $DIR/identity_op.rs:52:5
77+
|
78+
LL | x >> &0;
79+
| ^^^^^^^
80+
81+
error: aborting due to 13 previous errors
7682

0 commit comments

Comments
 (0)