Skip to content
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
10 changes: 9 additions & 1 deletion clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::sugg::has_enclosing_paren;
use clippy_utils::ty::{adjust_derefs_manually_drop, implements_trait, is_manually_drop, peel_and_count_ty_refs};
use clippy_utils::{
DefinedTy, ExprUseNode, expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local,
DefinedTy, ExprUseNode, expr_use_ctxt, get_parent_expr, is_block_like, is_from_proc_macro, is_lint_allowed,
path_to_local,
};
use rustc_ast::util::parser::ExprPrecedence;
use rustc_data_structures::fx::FxIndexMap;
Expand Down Expand Up @@ -260,6 +261,13 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
};
self.skip_expr = skip_expr;

if is_from_proc_macro(cx, expr) {
if let Some((state, data)) = self.state.take() {
report(cx, expr, state, data, cx.typeck_results());
}
return;
}

match (self.state.take(), kind) {
(None, kind) => {
let expr_ty = typeck.expr_ty(expr);
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/explicit_deref_methods.fixed
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@aux-build:proc_macros.rs
#![warn(clippy::explicit_deref_methods)]
#![allow(unused_variables, unused_must_use)]
#![allow(
Expand All @@ -14,6 +15,8 @@

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

extern crate proc_macros;

fn concat(deref_str: &str) -> String {
format!("{}bar", deref_str)
}
Expand Down Expand Up @@ -121,6 +124,18 @@ fn main() {
let b: &str = expr_deref!(&*a);
//~^ explicit_deref_methods

proc_macros::external! {
let a: &mut String = &mut String::from("foo");
let b: &str = a.deref();
}

// Issue #15168
proc_macros::with_span! {
span
let a: &mut String = &mut String::from("foo");
let b: &str = a.deref();
}

// The struct does not implement Deref trait
#[derive(Copy, Clone)]
struct NoLint(u32);
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/explicit_deref_methods.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@aux-build:proc_macros.rs
#![warn(clippy::explicit_deref_methods)]
#![allow(unused_variables, unused_must_use)]
#![allow(
Expand All @@ -14,6 +15,8 @@

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

extern crate proc_macros;

fn concat(deref_str: &str) -> String {
format!("{}bar", deref_str)
}
Expand Down Expand Up @@ -121,6 +124,18 @@ fn main() {
let b: &str = expr_deref!(a.deref());
//~^ explicit_deref_methods

proc_macros::external! {
let a: &mut String = &mut String::from("foo");
let b: &str = a.deref();
}

// Issue #15168
proc_macros::with_span! {
span
let a: &mut String = &mut String::from("foo");
let b: &str = a.deref();
}

// The struct does not implement Deref trait
#[derive(Copy, Clone)]
struct NoLint(u32);
Expand Down
26 changes: 13 additions & 13 deletions tests/ui/explicit_deref_methods.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:55:19
--> tests/ui/explicit_deref_methods.rs:58:19
|
LL | let b: &str = a.deref();
| ^^^^^^^^^ help: try: `&*a`
Expand All @@ -8,73 +8,73 @@ LL | let b: &str = a.deref();
= help: to override `-D warnings` add `#[allow(clippy::explicit_deref_methods)]`

error: explicit `deref_mut` method call
--> tests/ui/explicit_deref_methods.rs:58:23
--> tests/ui/explicit_deref_methods.rs:61:23
|
LL | let b: &mut str = a.deref_mut();
| ^^^^^^^^^^^^^ help: try: `&mut **a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:62:39
--> tests/ui/explicit_deref_methods.rs:65:39
|
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:62:50
--> tests/ui/explicit_deref_methods.rs:65:50
|
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:66:20
--> tests/ui/explicit_deref_methods.rs:69:20
|
LL | println!("{}", a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:70:11
--> tests/ui/explicit_deref_methods.rs:73:11
|
LL | match a.deref() {
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:75:28
--> tests/ui/explicit_deref_methods.rs:78:28
|
LL | let b: String = concat(a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:78:13
--> tests/ui/explicit_deref_methods.rs:81:13
|
LL | let b = just_return(a).deref();
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `just_return(a)`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:81:28
--> tests/ui/explicit_deref_methods.rs:84:28
|
LL | let b: String = concat(just_return(a).deref());
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `just_return(a)`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:121:31
--> tests/ui/explicit_deref_methods.rs:124:31
|
LL | let b: &str = expr_deref!(a.deref());
| ^^^^^^^^^ help: try: `&*a`

error: explicit `deref` method call
--> tests/ui/explicit_deref_methods.rs:139:14
--> tests/ui/explicit_deref_methods.rs:154:14
|
LL | let _ = &Deref::deref(&"foo");
| ^^^^^^^^^^^^^^^^^^^^ help: try: `*&"foo"`

error: explicit `deref_mut` method call
--> tests/ui/explicit_deref_methods.rs:141:14
--> tests/ui/explicit_deref_methods.rs:156:14
|
LL | let _ = &DerefMut::deref_mut(&mut x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut **&mut x`

error: explicit `deref_mut` method call
--> tests/ui/explicit_deref_methods.rs:142:14
--> tests/ui/explicit_deref_methods.rs:157:14
|
LL | let _ = &DerefMut::deref_mut((&mut &mut x).deref_mut());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut ***(&mut &mut x)`
Expand Down