Skip to content

Commit d558792

Browse files
committed
Better precedence case management + more tests
1 parent d6e3dc5 commit d558792

File tree

6 files changed

+49
-17
lines changed

6 files changed

+49
-17
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 361 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
8+
[There are 362 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

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{get_parent_expr, implements_trait, snippet, span_lint_and_sugg};
22
use if_chain::if_chain;
3-
use rustc_ast::util::parser::ExprPrecedence;
3+
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX, PREC_PREFIX};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
@@ -51,9 +51,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Dereferencing {
5151
if let ExprKind::MethodCall(..) = parent_expr.kind {
5252
return;
5353
}
54-
// Check for unary precedence
55-
if let ExprPrecedence::Unary = parent_expr.precedence() {
56-
return;
54+
// Check for Expr that we don't want to be linted
55+
let precedence = parent_expr.precedence();
56+
match precedence {
57+
// Lint a Call is ok though
58+
ExprPrecedence::Call | ExprPrecedence::AddrOf => (), // | ExprPrecedence::MethodCall
59+
_ => {
60+
if precedence.order() >= PREC_PREFIX && precedence.order() <= PREC_POSTFIX {
61+
// println!("Sup:\nexpr: {:#?}\nparent_expr: {:#?}\nprec: {:#?}", expr.span, parent_expr, precedence);
62+
return;
63+
}
64+
}
5765
}
5866
}
5967
let name = method_name.ident.as_str();

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; 361] = [
9+
pub const ALL_LINTS: [Lint; 362] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",

tests/ui/dereference.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ fn just_return(deref_str: &str) -> &str {
1313
deref_str
1414
}
1515

16+
struct CustomVec(Vec<u8>);
17+
impl Deref for CustomVec {
18+
type Target = Vec<u8>;
19+
20+
fn deref(&self) -> &Vec<u8> {
21+
&self.0
22+
}
23+
}
24+
1625
fn main() {
1726
let a: &mut String = &mut String::from("foo");
1827

@@ -45,6 +54,9 @@ fn main() {
4554

4655
// following should not require linting
4756

57+
let cv = CustomVec(vec![0, 42]);
58+
let c = cv.deref()[0];
59+
4860
let b: &str = &*a.deref();
4961

5062
let b: String = a.deref().clone();

tests/ui/dereference.rs

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ fn just_return(deref_str: &str) -> &str {
1313
deref_str
1414
}
1515

16+
struct CustomVec(Vec<u8>);
17+
impl Deref for CustomVec {
18+
type Target = Vec<u8>;
19+
20+
fn deref(&self) -> &Vec<u8> {
21+
&self.0
22+
}
23+
}
24+
1625
fn main() {
1726
let a: &mut String = &mut String::from("foo");
1827

@@ -45,6 +54,9 @@ fn main() {
4554

4655
// following should not require linting
4756

57+
let cv = CustomVec(vec![0, 42]);
58+
let c = cv.deref()[0];
59+
4860
let b: &str = &*a.deref();
4961

5062
let b: String = a.deref().clone();

tests/ui/dereference.stderr

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

99
error: explicit deref_mut method call
10-
--> $DIR/dereference.rs:23:23
10+
--> $DIR/dereference.rs:32: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:26:39
16+
--> $DIR/dereference.rs:35:39
1717
|
1818
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
1919
| ^^^^^^^^^ help: try this: `&*a`
2020

2121
error: explicit deref method call
22-
--> $DIR/dereference.rs:26:50
22+
--> $DIR/dereference.rs:35:50
2323
|
2424
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
2525
| ^^^^^^^^^ help: try this: `&*a`
2626

2727
error: explicit deref method call
28-
--> $DIR/dereference.rs:28:20
28+
--> $DIR/dereference.rs:37:20
2929
|
3030
LL | println!("{}", a.deref());
3131
| ^^^^^^^^^ help: try this: `&*a`
3232

3333
error: explicit deref method call
34-
--> $DIR/dereference.rs:31:11
34+
--> $DIR/dereference.rs:40:11
3535
|
3636
LL | match a.deref() {
3737
| ^^^^^^^^^ help: try this: `&*a`
3838

3939
error: explicit deref method call
40-
--> $DIR/dereference.rs:35:28
40+
--> $DIR/dereference.rs:44:28
4141
|
4242
LL | let b: String = concat(a.deref());
4343
| ^^^^^^^^^ help: try this: `&*a`
4444

4545
error: explicit deref method call
46-
--> $DIR/dereference.rs:37:13
46+
--> $DIR/dereference.rs:46:13
4747
|
4848
LL | let b = just_return(a).deref();
4949
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&*just_return(a)`
5050

5151
error: explicit deref method call
52-
--> $DIR/dereference.rs:39:28
52+
--> $DIR/dereference.rs:48:28
5353
|
5454
LL | let b: String = concat(just_return(a).deref());
5555
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&*just_return(a)`
5656

5757
error: explicit deref method call
58-
--> $DIR/dereference.rs:41:19
58+
--> $DIR/dereference.rs:50:19
5959
|
6060
LL | let b: &str = a.deref().deref();
6161
| ^^^^^^^^^^^^^^^^^ help: try this: `&*a.deref()`
6262

6363
error: explicit deref method call
64-
--> $DIR/dereference.rs:44:13
64+
--> $DIR/dereference.rs:53:13
6565
|
6666
LL | let b = opt_a.unwrap().deref();
6767
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&*opt_a.unwrap()`

0 commit comments

Comments
 (0)