Skip to content

Fix op_ref false positives #1671

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 4 commits into from
Apr 10, 2017
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
123 changes: 79 additions & 44 deletions clippy_lints/src/eq_op.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc::hir::*;
use rustc::lint::*;
use utils::{SpanlessEq, span_lint, span_lint_and_then, multispan_sugg, snippet};
use utils::{SpanlessEq, span_lint, span_lint_and_then, multispan_sugg, snippet, implements_trait};
use utils::sugg::Sugg;

/// **What it does:** Checks for equal operands to comparison, logical and
Expand Down Expand Up @@ -60,53 +60,88 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
e.span,
&format!("equal expressions as operands to `{}`", op.node.as_str()));
} else {
match (&left.node, &right.node) {
(&ExprAddrOf(_, ref l), &ExprAddrOf(_, ref r)) => {
span_lint_and_then(cx,
OP_REF,
e.span,
"taken reference of both operands, which is done automatically by the operator anyway",
|db| {
let lsnip = snippet(cx, l.span, "...").to_string();
let rsnip = snippet(cx, r.span, "...").to_string();
multispan_sugg(db,
"use the values directly".to_string(),
vec![(left.span, lsnip),
(right.span, rsnip)]);
let trait_id = match op.node {
BiAdd => cx.tcx.lang_items.add_trait(),
BiSub => cx.tcx.lang_items.sub_trait(),
BiMul => cx.tcx.lang_items.mul_trait(),
BiDiv => cx.tcx.lang_items.div_trait(),
BiRem => cx.tcx.lang_items.rem_trait(),
BiAnd |
BiOr => None,
BiBitXor => cx.tcx.lang_items.bitxor_trait(),
BiBitAnd => cx.tcx.lang_items.bitand_trait(),
BiBitOr => cx.tcx.lang_items.bitor_trait(),
BiShl => cx.tcx.lang_items.shl_trait(),
BiShr => cx.tcx.lang_items.shr_trait(),
BiNe |
BiEq => cx.tcx.lang_items.eq_trait(),
BiLt |
BiLe |
BiGe |
BiGt => cx.tcx.lang_items.ord_trait(),
};
if let Some(trait_id) = trait_id {
#[allow(match_same_arms)]
match (&left.node, &right.node) {
// do not suggest to dereference literals
(&ExprLit(..), _) |
(_, &ExprLit(..)) => {},
// &foo == &bar
(&ExprAddrOf(_, ref l), &ExprAddrOf(_, ref r)) => {
if implements_trait(cx, cx.tables.expr_ty(l), trait_id, &[cx.tables.expr_ty(r)], None) {
span_lint_and_then(cx,
OP_REF,
e.span,
"taken reference of both operands, which is done automatically by the operator anyway",
|db| {
let lsnip = snippet(cx, l.span, "...").to_string();
let rsnip = snippet(cx, r.span, "...").to_string();
multispan_sugg(db,
"use the values directly".to_string(),
vec![(left.span, lsnip),
(right.span, rsnip)]);
}
)
}
)
}
(&ExprAddrOf(_, ref l), _) => {
span_lint_and_then(cx,
OP_REF,
e.span,
"taken reference of left operand",
|db| {
let lsnip = snippet(cx, l.span, "...").to_string();
let rsnip = Sugg::hir(cx, right, "...").deref().to_string();
multispan_sugg(db,
"dereference the right operand instead".to_string(),
vec![(left.span, lsnip),
(right.span, rsnip)]);
}
// &foo == bar
(&ExprAddrOf(_, ref l), _) => {
if implements_trait(cx, cx.tables.expr_ty(l), trait_id, &[cx.tables.expr_ty(right)], None) {
span_lint_and_then(cx,
OP_REF,
e.span,
"taken reference of left operand",
|db| {
let lsnip = snippet(cx, l.span, "...").to_string();
let rsnip = Sugg::hir(cx, right, "...").deref().to_string();
multispan_sugg(db,
"dereference the right operand instead".to_string(),
vec![(left.span, lsnip),
(right.span, rsnip)]);
}
)
}
)
}
(_, &ExprAddrOf(_, ref r)) => {
span_lint_and_then(cx,
OP_REF,
e.span,
"taken reference of right operand",
|db| {
let lsnip = Sugg::hir(cx, left, "...").deref().to_string();
let rsnip = snippet(cx, r.span, "...").to_string();
multispan_sugg(db,
"dereference the left operand instead".to_string(),
vec![(left.span, lsnip),
(right.span, rsnip)]);
}
// foo == &bar
(_, &ExprAddrOf(_, ref r)) => {
if implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[cx.tables.expr_ty(r)], None) {
span_lint_and_then(cx,
OP_REF,
e.span,
"taken reference of right operand",
|db| {
let lsnip = Sugg::hir(cx, left, "...").deref().to_string();
let rsnip = snippet(cx, r.span, "...").to_string();
multispan_sugg(db,
"dereference the left operand instead".to_string(),
vec![(left.span, lsnip),
(right.span, rsnip)]);
}
)
}
)
}
_ => {}
}
_ => {}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn get_argument_fmtstr_parts<'a, 'b>(cx: &LateContext<'a, 'b>, expr: &'a Exp

/// Checks if the expressions matches
/// ```rust
/// { static __STATIC_FMTSTR: = &["", "", ]; __STATIC_FMTSTR }
/// { static __STATIC_FMTSTR: s = &["a", "b", c]; __STATIC_FMTSTR }
/// ```
fn check_static_str(cx: &LateContext, expr: &Expr) -> bool {
if let Some(expr) = get_argument_fmtstr_parts(cx, expr) {
Expand All @@ -110,10 +110,10 @@ fn check_static_str(cx: &LateContext, expr: &Expr) -> bool {
}

/// Checks if the expressions matches
/// ```rust
/// ```rust,ignore
/// &match (&42,) {
/// (__arg0,) => [::std::fmt::ArgumentV1::new(__arg0, ::std::fmt::Display::fmt)],
/// })
/// }
/// ```
fn check_arg_is_display(cx: &LateContext, expr: &Expr) -> bool {
if_let_chain! {[
Expand Down
79 changes: 53 additions & 26 deletions tests/ui/collapsible_if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ error: this if statement can be collapsed
9 | |
10 | |
11 | |
12 | | if y == "world" {
13 | | println!("Hello world!");
... |
14 | | }
15 | | }
| |_____^ ...ending here
Expand All @@ -30,8 +29,7 @@ error: this if statement can be collapsed
18 | |
19 | |
20 | |
21 | | if y == "world" || y == "hello" {
22 | | println!("Hello world!");
... |
23 | | }
24 | | }
| |_____^ ...ending here
Expand All @@ -49,8 +47,7 @@ error: this if statement can be collapsed
27 | |
28 | |
29 | |
30 | | if y == "world" || y == "hello" {
31 | | println!("Hello world!");
... |
32 | | }
33 | | }
| |_____^ ...ending here
Expand All @@ -68,8 +65,7 @@ error: this if statement can be collapsed
36 | |
37 | |
38 | |
39 | | if y == "world" && y == "hello" {
40 | | println!("Hello world!");
... |
41 | | }
42 | | }
| |_____^ ...ending here
Expand All @@ -87,8 +83,7 @@ error: this if statement can be collapsed
45 | |
46 | |
47 | |
48 | | if y == "world" && y == "hello" {
49 | | println!("Hello world!");
... |
50 | | }
51 | | }
| |_____^ ...ending here
Expand All @@ -106,8 +101,7 @@ error: this if statement can be collapsed
54 | |
55 | |
56 | |
57 | | if 'a' != 'A' {
58 | | println!("world!")
... |
59 | | }
60 | | }
| |_____^ ...ending here
Expand All @@ -125,8 +119,7 @@ error: this `else { if .. }` block can be collapsed
66 | |
67 | |
68 | |
69 | | if y == "world" {
70 | | println!("world!")
... |
71 | | }
72 | | }
| |_____^ ...ending here
Expand All @@ -144,8 +137,7 @@ error: this `else { if .. }` block can be collapsed
77 | |
78 | |
79 | |
80 | | if let Some(42) = Some(42) {
81 | | println!("world!")
... |
82 | | }
83 | | }
| |_____^ ...ending here
Expand All @@ -158,8 +150,15 @@ help: try
error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:87:12
|
87 | } else {
| ^
87 | } else {
| ____________^ starting here...
88 | |
89 | |
90 | |
... |
96 | | }
97 | | }
| |_____^ ...ending here
|
help: try
| } else if y == "world" {
Expand All @@ -172,8 +171,15 @@ help: try
error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:101:12
|
101 | } else {
| ^
101 | } else {
| ____________^ starting here...
102 | |
103 | |
104 | |
... |
110 | | }
111 | | }
| |_____^ ...ending here
|
help: try
| } else if let Some(42) = Some(42) {
Expand All @@ -186,8 +192,15 @@ help: try
error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:115:12
|
115 | } else {
| ^
115 | } else {
| ____________^ starting here...
116 | |
117 | |
118 | |
... |
124 | | }
125 | | }
| |_____^ ...ending here
|
help: try
| } else if let Some(42) = Some(42) {
Expand All @@ -200,8 +213,15 @@ help: try
error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:129:12
|
129 | } else {
| ^
129 | } else {
| ____________^ starting here...
130 | |
131 | |
132 | |
... |
138 | | }
139 | | }
| |_____^ ...ending here
|
help: try
| } else if x == "hello" {
Expand All @@ -214,8 +234,15 @@ help: try
error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_if.rs:143:12
|
143 | } else {
| ^
143 | } else {
| ____________^ starting here...
144 | |
145 | |
146 | |
... |
152 | | }
153 | | }
| |_____^ ...ending here
|
help: try
| } else if let Some(42) = Some(42) {
Expand Down
Loading