Skip to content

Commit ef81e60

Browse files
committed
Global rework + fix imports
1 parent a2d1b0d commit ef81e60

File tree

6 files changed

+89
-93
lines changed

6 files changed

+89
-93
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,7 @@ Released 2018-09-13
11351135
[`expect_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
11361136
[`expl_impl_clone_on_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#expl_impl_clone_on_copy
11371137
[`explicit_counter_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_counter_loop
1138+
[`explicit_deref_method`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_deref_method
11381139
[`explicit_into_iter_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_into_iter_loop
11391140
[`explicit_iter_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_iter_loop
11401141
[`explicit_write`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_write

clippy_lints/src/dereference.rs

+33-27
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
1-
use crate::rustc::hir::{Expr, ExprKind, QPath};
2-
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
3-
use crate::rustc::{declare_tool_lint, lint_array};
1+
use rustc_hir::{Expr, ExprKind, QPath};
2+
use rustc_errors::Applicability;
3+
use rustc_lint::{LateContext, LateLintPass};
4+
use rustc_session::{declare_tool_lint, declare_lint_pass};
45
use crate::utils::{in_macro, span_lint_and_sugg};
56
use if_chain::if_chain;
67

7-
/// **What it does:** Checks for explicit deref() or deref_mut() method calls.
8-
///
9-
/// **Why is this bad?** Derefencing by &*x or &mut *x is clearer and more concise,
10-
/// when not part of a method chain.
11-
///
12-
/// **Example:**
13-
/// ```rust
14-
/// let b = a.deref();
15-
/// let c = a.deref_mut();
16-
///
17-
/// // excludes
18-
/// let e = d.unwrap().deref();
19-
/// ```
208
declare_clippy_lint! {
9+
/// **What it does:** Checks for explicit `deref()` or `deref_mut()` method calls.
10+
///
11+
/// **Why is this bad?** Derefencing by `&*x` or `&mut *x` is clearer and more concise,
12+
/// when not part of a method chain.
13+
///
14+
/// **Example:**
15+
/// ```rust
16+
/// let b = a.deref();
17+
/// let c = a.deref_mut();
18+
/// ```
19+
/// Could be written as:
20+
/// ```rust
21+
/// let b = &*a;
22+
/// let c = &mut *a;
23+
/// ```
24+
///
25+
/// This lint excludes
26+
/// ```rust
27+
/// let e = d.unwrap().deref();
28+
/// ```
2129
pub EXPLICIT_DEREF_METHOD,
2230
pedantic,
2331
"Explicit use of deref or deref_mut method while not in a method chain."
2432
}
2533

26-
pub struct Pass;
34+
declare_lint_pass!(Dereferencing => [
35+
EXPLICIT_DEREF_METHOD
36+
]);
2737

28-
impl LintPass for Pass {
29-
fn get_lints(&self) -> LintArray {
30-
lint_array!(EXPLICIT_DEREF_METHOD)
31-
}
32-
}
33-
34-
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
35-
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr) {
38+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Dereferencing {
39+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
3640
if in_macro(expr.span) {
3741
return;
3842
}
3943

4044
if_chain! {
4145
// if this is a method call
42-
if let ExprKind::MethodCall(ref method_name, _, ref args) = &expr.node;
46+
if let ExprKind::MethodCall(ref method_name, _, ref args) = &expr.kind;
4347
// on a Path (i.e. a variable/name, not another method)
44-
if let ExprKind::Path(QPath::Resolved(None, path)) = &args[0].node;
48+
if let ExprKind::Path(QPath::Resolved(None, path)) = &args[0].kind;
4549
then {
4650
let name = method_name.ident.as_str();
4751
// alter help slightly to account for _mut
@@ -54,6 +58,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
5458
"explicit deref method call",
5559
"try this",
5660
format!("&*{}", path),
61+
Applicability::MachineApplicable
5762
);
5863
},
5964
"deref_mut" => {
@@ -64,6 +69,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
6469
"explicit deref_mut method call",
6570
"try this",
6671
format!("&mut *{}", path),
72+
Applicability::MachineApplicable
6773
);
6874
},
6975
_ => ()

clippy_lints/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ pub fn read_conf(args: &[rustc_ast::ast::NestedMetaItem], sess: &Session) -> Con
384384
}
385385

386386
conf
387-
}
387+
},
388388
Err((err, span)) => {
389389
sess.struct_span_err(span, err)
390390
.span_note(span, "Clippy will use default configuration")
@@ -502,7 +502,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
502502
&copy_iterator::COPY_ITERATOR,
503503
&dbg_macro::DBG_MACRO,
504504
&default_trait_access::DEFAULT_TRAIT_ACCESS,
505-
&dereference::DEREF_METHOD_EXPLICIT,
505+
&dereference::EXPLICIT_DEREF_METHOD,
506506
&derive::DERIVE_HASH_XOR_EQ,
507507
&derive::EXPL_IMPL_CLONE_ON_COPY,
508508
&doc::DOC_MARKDOWN,
@@ -1013,7 +1013,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10131013
store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools));
10141014
store.register_early_pass(|| box option_env_unwrap::OptionEnvUnwrap);
10151015
store.register_late_pass(|| box wildcard_imports::WildcardImports);
1016-
store.register_late_pass(|| box dereference::DerefMethodExplicit);
1016+
store.register_late_pass(|| box dereference::Dereferencing);
10171017

10181018
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10191019
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1147,7 +1147,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11471147
LintId::of(&comparison_chain::COMPARISON_CHAIN),
11481148
LintId::of(&copies::IFS_SAME_COND),
11491149
LintId::of(&copies::IF_SAME_THEN_ELSE),
1150-
LintId::of(&dereference::EXPLICIT_DEREF_METHOD),
11511150
LintId::of(&derive::DERIVE_HASH_XOR_EQ),
11521151
LintId::of(&doc::MISSING_SAFETY_DOC),
11531152
LintId::of(&doc::NEEDLESS_DOCTEST_MAIN),

src/lintlist/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,13 @@ pub const ALL_LINTS: [Lint; 358] = [
525525
deprecation: None,
526526
module: "loops",
527527
},
528+
Lint {
529+
name: "explicit_deref_method",
530+
group: "pedantic",
531+
desc: "Explicit use of deref or deref_mut method while not in a method chain.",
532+
deprecation: None,
533+
module: "dereference",
534+
},
528535
Lint {
529536
name: "explicit_into_iter_loop",
530537
group: "pedantic",

tests/ui/dereference.rs

+21-38
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
1-
#![feature(tool_lints)]
1+
#![allow(unused_variables, clippy::many_single_char_names, clippy::clone_double_ref)]
2+
#![warn(clippy::explicit_deref_method)]
23

34
use std::ops::{Deref, DerefMut};
45

5-
#[allow(clippy::many_single_char_names, clippy::clone_double_ref)]
6-
#[allow(unused_variables)]
7-
#[warn(clippy::explicit_deref_method)]
86
fn main() {
97
let a: &mut String = &mut String::from("foo");
108

119
// these should require linting
12-
{
13-
let b: &str = a.deref();
14-
}
10+
let b: &str = a.deref();
1511

16-
{
17-
let b: &mut str = a.deref_mut();
18-
}
12+
let b: &mut str = a.deref_mut();
1913

20-
{
21-
let b: String = a.deref().clone();
22-
}
23-
24-
{
25-
let b: usize = a.deref_mut().len();
26-
}
27-
28-
{
29-
let b: &usize = &a.deref().len();
30-
}
14+
let b: String = a.deref().clone();
3115

32-
{
33-
// only first deref should get linted here
34-
let b: &str = a.deref().deref();
35-
}
16+
let b: usize = a.deref_mut().len();
3617

37-
{
38-
// both derefs should get linted here
39-
let b: String = format!("{}, {}", a.deref(), a.deref());
40-
}
18+
let b: &usize = &a.deref().len();
19+
20+
// only first deref should get linted here
21+
let b: &str = a.deref().deref();
22+
23+
// both derefs should get linted here
24+
let b: String = format!("{}, {}", a.deref(), a.deref());
4125

4226
// these should not require linting
43-
{
44-
let b: &str = &*a;
45-
}
4627

47-
{
48-
let b: &mut str = &mut *a;
49-
}
28+
let b: &str = &*a;
29+
30+
let b: &mut str = &mut *a;
5031

51-
{
52-
macro_rules! expr_deref { ($body:expr) => { $body.deref() } }
53-
let b: &str = expr_deref!(a);
32+
macro_rules! expr_deref {
33+
($body:expr) => {
34+
$body.deref()
35+
};
5436
}
37+
let b: &str = expr_deref!(a);
5538
}

tests/ui/dereference.stderr

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
error: explicit deref method call
2-
--> $DIR/dereference.rs:13:23
2+
--> $DIR/dereference.rs:10:19
33
|
4-
13 | let b: &str = a.deref();
5-
| ^^^^^^^^^ help: try this: `&*a`
4+
LL | let b: &str = a.deref();
5+
| ^^^^^^^^^ 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:17:27
10+
--> $DIR/dereference.rs:12:23
1111
|
12-
17 | let b: &mut str = a.deref_mut();
13-
| ^^^^^^^^^^^^^ help: try this: `&mut *a`
12+
LL | let b: &mut str = a.deref_mut();
13+
| ^^^^^^^^^^^^^ help: try this: `&mut *a`
1414

1515
error: explicit deref method call
16-
--> $DIR/dereference.rs:21:25
16+
--> $DIR/dereference.rs:14:21
1717
|
18-
21 | let b: String = a.deref().clone();
19-
| ^^^^^^^^^ help: try this: `&*a`
18+
LL | let b: String = a.deref().clone();
19+
| ^^^^^^^^^ help: try this: `&*a`
2020

2121
error: explicit deref_mut method call
22-
--> $DIR/dereference.rs:25:24
22+
--> $DIR/dereference.rs:16:20
2323
|
24-
25 | let b: usize = a.deref_mut().len();
25-
| ^^^^^^^^^^^^^ help: try this: `&mut *a`
24+
LL | let b: usize = a.deref_mut().len();
25+
| ^^^^^^^^^^^^^ help: try this: `&mut *a`
2626

2727
error: explicit deref method call
28-
--> $DIR/dereference.rs:29:26
28+
--> $DIR/dereference.rs:18:22
2929
|
30-
29 | let b: &usize = &a.deref().len();
31-
| ^^^^^^^^^ help: try this: `&*a`
30+
LL | let b: &usize = &a.deref().len();
31+
| ^^^^^^^^^ help: try this: `&*a`
3232

3333
error: explicit deref method call
34-
--> $DIR/dereference.rs:34:23
34+
--> $DIR/dereference.rs:21:19
3535
|
36-
34 | let b: &str = a.deref().deref();
37-
| ^^^^^^^^^ help: try this: `&*a`
36+
LL | let b: &str = a.deref().deref();
37+
| ^^^^^^^^^ help: try this: `&*a`
3838

3939
error: explicit deref method call
40-
--> $DIR/dereference.rs:39:43
40+
--> $DIR/dereference.rs:24:39
4141
|
42-
39 | let b: String = format!("{}, {}", a.deref(), a.deref());
43-
| ^^^^^^^^^ help: try this: `&*a`
42+
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
43+
| ^^^^^^^^^ help: try this: `&*a`
4444

4545
error: explicit deref method call
46-
--> $DIR/dereference.rs:39:54
46+
--> $DIR/dereference.rs:24:50
4747
|
48-
39 | let b: String = format!("{}, {}", a.deref(), a.deref());
49-
| ^^^^^^^^^ help: try this: `&*a`
48+
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
49+
| ^^^^^^^^^ help: try this: `&*a`
5050

5151
error: aborting due to 8 previous errors
5252

0 commit comments

Comments
 (0)