Skip to content

Commit 6b31e9c

Browse files
committed
new test
1 parent d21ef63 commit 6b31e9c

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

clippy_lints/src/needless_deref.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::in_macro;
33
use clippy_utils::source::snippet_opt;
4-
use rustc_hir::ExprKind;
5-
use rustc_hir::UnOp;
4+
use rustc_hir::{ExprKind, UnOp};
65
use rustc_lint::{LateContext, LateLintPass};
76
use rustc_middle::mir::Mutability;
87
use rustc_middle::ty;
@@ -51,13 +50,13 @@ impl LateLintPass<'_> for NeedlessDeref {
5150
if let ty::Ref(_, _, Mutability::Not) = inner_ty.kind();
5251
then{
5352

54-
let map=cx.tcx.hir();
53+
let map = cx.tcx.hir();
5554
if let Some(parent_hir_id) = map.find_parent_node(e.hir_id){
5655
let span=map.span(parent_hir_id);
5756
if span.from_expansion() || in_macro(span) {
5857
return;
5958
}
60-
let parent_node=map.find(parent_hir_id);
59+
let parent_node = map.find(parent_hir_id);
6160
if let Some(rustc_hir::Node::Expr(parent_expr)) = parent_node {
6261
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) ||
6362
matches!(parent_expr.kind, ExprKind::AddrOf(..) ){
@@ -66,7 +65,6 @@ impl LateLintPass<'_> for NeedlessDeref {
6665
}
6766
}
6867

69-
7068
span_lint_and_help(
7169
cx,
7270
NEEDLESS_DEREF,
@@ -75,7 +73,7 @@ impl LateLintPass<'_> for NeedlessDeref {
7573
None,
7674
&format!(
7775
"consider using `{}` if you would like to deref\nconsider using `{}` if you would like to reborrow",
78-
"&**".to_owned()+&snippet_opt(cx, deref_target.span).unwrap(),
76+
"&**".to_owned() + &snippet_opt(cx, deref_target.span).unwrap(),
7977
&snippet_opt(cx, deref_target.span).unwrap(),
8078
)
8179
);

tests/ui/needless_deref.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ mod should_lint {
44
fn foo() {
55
let a = &12;
66
let _b = &*a;
7+
8+
let s = &String::new();
9+
let a: &str = &*s;
710
}
811
}
912

tests/ui/needless_deref.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,14 @@ LL | let _b = &*a;
88
= help: consider using `&**a` if you would like to deref
99
consider using `a` if you would like to reborrow
1010

11-
error: aborting due to previous error
11+
error: reference on a de-referenced
12+
--> $DIR/needless_deref.rs:9:23
13+
|
14+
LL | let a: &str = &*s;
15+
| ^^^
16+
|
17+
= help: consider using `&**s` if you would like to deref
18+
consider using `s` if you would like to reborrow
19+
20+
error: aborting due to 2 previous errors
1221

0 commit comments

Comments
 (0)