Skip to content

Commit a034f20

Browse files
committed
Report using stmts and expr + tests
1 parent ef81e60 commit a034f20

File tree

5 files changed

+150
-70
lines changed

5 files changed

+150
-70
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
77

8-
[There are 358 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
8+
[There are 359 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
99

1010
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1111

clippy_lints/src/dereference.rs

+100-38
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use rustc_hir::{Expr, ExprKind, QPath};
1+
use crate::utils::{get_parent_expr, method_calls, snippet, span_lint_and_sugg};
2+
use if_chain::if_chain;
23
use rustc_errors::Applicability;
4+
use rustc_hir as hir;
5+
use rustc_hir::{Expr, ExprKind, QPath, StmtKind};
36
use rustc_lint::{LateContext, LateLintPass};
4-
use rustc_session::{declare_tool_lint, declare_lint_pass};
5-
use crate::utils::{in_macro, span_lint_and_sugg};
6-
use if_chain::if_chain;
7+
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_span::source_map::Span;
79

810
declare_clippy_lint! {
911
/// **What it does:** Checks for explicit `deref()` or `deref_mut()` method calls.
@@ -21,7 +23,7 @@ declare_clippy_lint! {
2123
/// let b = &*a;
2224
/// let c = &mut *a;
2325
/// ```
24-
///
26+
///
2527
/// This lint excludes
2628
/// ```rust
2729
/// let e = d.unwrap().deref();
@@ -36,45 +38,105 @@ declare_lint_pass!(Dereferencing => [
3638
]);
3739

3840
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Dereferencing {
39-
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
40-
if in_macro(expr.span) {
41-
return;
41+
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx hir::Stmt<'_>) {
42+
if_chain! {
43+
if let StmtKind::Local(ref local) = stmt.kind;
44+
if let Some(ref init) = local.init;
45+
46+
then {
47+
match init.kind {
48+
ExprKind::Call(ref _method, args) => {
49+
for arg in args {
50+
if_chain! {
51+
// Caller must call only one other function (deref or deref_mut)
52+
// otherwise it can lead to error prone suggestions (ex: &*a.len())
53+
let (method_names, arg_list, _) = method_calls(arg, 2);
54+
if method_names.len() == 1;
55+
// Caller must be a variable
56+
let variables = arg_list[0];
57+
if variables.len() == 1;
58+
if let ExprKind::Path(QPath::Resolved(None, _)) = variables[0].kind;
59+
60+
then {
61+
let name = method_names[0].as_str();
62+
lint_deref(cx, &*name, variables[0].span, arg.span);
63+
}
64+
}
65+
}
66+
}
67+
ExprKind::MethodCall(ref method_name, _, ref args) => {
68+
if init.span.from_expansion() {
69+
return;
70+
}
71+
if_chain! {
72+
if args.len() == 1;
73+
if let ExprKind::Path(QPath::Resolved(None, _)) = args[0].kind;
74+
// Caller must call only one other function (deref or deref_mut)
75+
// otherwise it can lead to error prone suggestions (ex: &*a.len())
76+
let (method_names, arg_list, _) = method_calls(init, 2);
77+
if method_names.len() == 1;
78+
// Caller must be a variable
79+
let variables = arg_list[0];
80+
if variables.len() == 1;
81+
if let ExprKind::Path(QPath::Resolved(None, _)) = variables[0].kind;
82+
83+
then {
84+
let name = method_name.ident.as_str();
85+
lint_deref(cx, &*name, args[0].span, init.span);
86+
}
87+
}
88+
}
89+
_ => ()
90+
}
91+
}
4292
}
93+
}
4394

95+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
4496
if_chain! {
45-
// if this is a method call
4697
if let ExprKind::MethodCall(ref method_name, _, ref args) = &expr.kind;
47-
// on a Path (i.e. a variable/name, not another method)
48-
if let ExprKind::Path(QPath::Resolved(None, path)) = &args[0].kind;
98+
if args.len() == 1;
99+
if let Some(parent) = get_parent_expr(cx, &expr);
100+
49101
then {
50-
let name = method_name.ident.as_str();
51-
// alter help slightly to account for _mut
52-
match &*name {
53-
"deref" => {
54-
span_lint_and_sugg(
55-
cx,
56-
EXPLICIT_DEREF_METHOD,
57-
expr.span,
58-
"explicit deref method call",
59-
"try this",
60-
format!("&*{}", path),
61-
Applicability::MachineApplicable
62-
);
63-
},
64-
"deref_mut" => {
65-
span_lint_and_sugg(
66-
cx,
67-
EXPLICIT_DEREF_METHOD,
68-
expr.span,
69-
"explicit deref_mut method call",
70-
"try this",
71-
format!("&mut *{}", path),
72-
Applicability::MachineApplicable
73-
);
74-
},
75-
_ => ()
76-
};
102+
// Call and MethodCall exprs are better reported using statements
103+
match parent.kind {
104+
ExprKind::Call(_, _) => return,
105+
ExprKind::MethodCall(_, _, _) => return,
106+
_ => {
107+
let name = method_name.ident.as_str();
108+
lint_deref(cx, &*name, args[0].span, expr.span);
109+
}
110+
}
77111
}
78112
}
79113
}
80114
}
115+
116+
fn lint_deref(cx: &LateContext<'_, '_>, fn_name: &str, var_span: Span, expr_span: Span) {
117+
match fn_name {
118+
"deref" => {
119+
span_lint_and_sugg(
120+
cx,
121+
EXPLICIT_DEREF_METHOD,
122+
expr_span,
123+
"explicit deref method call",
124+
"try this",
125+
format!("&*{}", &snippet(cx, var_span, "..")),
126+
Applicability::MachineApplicable,
127+
);
128+
},
129+
"deref_mut" => {
130+
span_lint_and_sugg(
131+
cx,
132+
EXPLICIT_DEREF_METHOD,
133+
expr_span,
134+
"explicit deref_mut method call",
135+
"try this",
136+
format!("&mut *{}", &snippet(cx, var_span, "..")),
137+
Applicability::MachineApplicable,
138+
);
139+
},
140+
_ => (),
141+
}
142+
}

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 358] = [
9+
pub const ALL_LINTS: [Lint; 359] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",

tests/ui/dereference.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,49 @@
33

44
use std::ops::{Deref, DerefMut};
55

6+
fn concat(deref_str: &str) -> String {
7+
format!("{}bar", deref_str)
8+
}
9+
10+
fn just_return(deref_str: &str) -> &str {
11+
deref_str
12+
}
13+
614
fn main() {
715
let a: &mut String = &mut String::from("foo");
816

917
// these should require linting
18+
1019
let b: &str = a.deref();
1120

1221
let b: &mut str = a.deref_mut();
1322

23+
// both derefs should get linted here
24+
let b: String = format!("{}, {}", a.deref(), a.deref());
25+
26+
println!("{}", a.deref());
27+
28+
#[allow(clippy::match_single_binding)]
29+
match a.deref() {
30+
_ => (),
31+
}
32+
33+
let b: String = concat(a.deref());
34+
35+
// following should not require linting
36+
37+
let b = just_return(a).deref();
38+
39+
let b: String = concat(just_return(a).deref());
40+
1441
let b: String = a.deref().clone();
1542

1643
let b: usize = a.deref_mut().len();
1744

1845
let b: &usize = &a.deref().len();
1946

20-
// only first deref should get linted here
2147
let b: &str = a.deref().deref();
2248

23-
// both derefs should get linted here
24-
let b: String = format!("{}, {}", a.deref(), a.deref());
25-
26-
// these should not require linting
27-
2849
let b: &str = &*a;
2950

3051
let b: &mut str = &mut *a;
@@ -35,4 +56,7 @@ fn main() {
3556
};
3657
}
3758
let b: &str = expr_deref!(a);
59+
60+
let opt_a = Some(a);
61+
let b = opt_a.unwrap().deref();
3862
}

tests/ui/dereference.stderr

+18-24
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,46 @@
11
error: explicit deref method call
2-
--> $DIR/dereference.rs:10:19
2+
--> $DIR/dereference.rs:19:19
33
|
44
LL | let b: &str = a.deref();
55
| ^^^^^^^^^ help: try this: `&*a`
66
|
77
= note: `-D clippy::explicit-deref-method` implied by `-D warnings`
88

99
error: explicit deref_mut method call
10-
--> $DIR/dereference.rs:12:23
10+
--> $DIR/dereference.rs:21:23
1111
|
1212
LL | let b: &mut str = a.deref_mut();
1313
| ^^^^^^^^^^^^^ help: try this: `&mut *a`
1414

1515
error: explicit deref method call
16-
--> $DIR/dereference.rs:14:21
17-
|
18-
LL | let b: String = a.deref().clone();
19-
| ^^^^^^^^^ help: try this: `&*a`
20-
21-
error: explicit deref_mut method call
22-
--> $DIR/dereference.rs:16:20
16+
--> $DIR/dereference.rs:24:39
2317
|
24-
LL | let b: usize = a.deref_mut().len();
25-
| ^^^^^^^^^^^^^ help: try this: `&mut *a`
18+
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
19+
| ^^^^^^^^^ help: try this: `&*a`
2620

2721
error: explicit deref method call
28-
--> $DIR/dereference.rs:18:22
22+
--> $DIR/dereference.rs:24:50
2923
|
30-
LL | let b: &usize = &a.deref().len();
31-
| ^^^^^^^^^ help: try this: `&*a`
24+
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
25+
| ^^^^^^^^^ help: try this: `&*a`
3226

3327
error: explicit deref method call
34-
--> $DIR/dereference.rs:21:19
28+
--> $DIR/dereference.rs:26:20
3529
|
36-
LL | let b: &str = a.deref().deref();
37-
| ^^^^^^^^^ help: try this: `&*a`
30+
LL | println!("{}", a.deref());
31+
| ^^^^^^^^^ help: try this: `&*a`
3832

3933
error: explicit deref method call
40-
--> $DIR/dereference.rs:24:39
34+
--> $DIR/dereference.rs:29:11
4135
|
42-
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
43-
| ^^^^^^^^^ help: try this: `&*a`
36+
LL | match a.deref() {
37+
| ^^^^^^^^^ help: try this: `&*a`
4438

4539
error: explicit deref method call
46-
--> $DIR/dereference.rs:24:50
40+
--> $DIR/dereference.rs:33:28
4741
|
48-
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
49-
| ^^^^^^^^^ help: try this: `&*a`
42+
LL | let b: String = concat(a.deref());
43+
| ^^^^^^^^^ help: try this: `&*a`
5044

51-
error: aborting due to 8 previous errors
45+
error: aborting due to 7 previous errors
5246

0 commit comments

Comments
 (0)