Skip to content

Commit

Permalink
needless_borrow: print the type in the lint message
Browse files Browse the repository at this point in the history
changelog: needless_borrow: print type in lint message
  • Loading branch information
matthiaskrgr committed Dec 13, 2020
1 parent b7db5bf commit 144ee13
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ path = "src/main.rs"
name = "clippy-driver"
path = "src/driver.rs"

[target.'cfg(NOT_A_PLATFORM)'.dependencies]
rustc_driver = { path = "/home/matthias/vcs/github/rust/compiler/rustc_driver" }
rustc_errors = { path = "/home/matthias/vcs/github/rust/compiler/rustc_errors" }
rustc_interface = { path = "/home/matthias/vcs/github/rust/compiler/rustc_interface" }
rustc_middle = { path = "/home/matthias/vcs/github/rust/compiler/rustc_middle" }

[dependencies]
# begin automatic update
clippy_lints = { version = "0.0.212", path = "clippy_lints" }
Expand Down
22 changes: 22 additions & 0 deletions clippy_lints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ license = "MIT OR Apache-2.0"
keywords = ["clippy", "lint", "plugin"]
edition = "2018"

[target.'cfg(NOT_A_PLATFORM)'.dependencies]
rustc_ast = { path = "/home/matthias/vcs/github/rust/compiler/rustc_ast" }
rustc_ast_pretty = { path = "/home/matthias/vcs/github/rust/compiler/rustc_ast_pretty" }
rustc_attr = { path = "/home/matthias/vcs/github/rust/compiler/rustc_attr" }
rustc_data_structures = { path = "/home/matthias/vcs/github/rust/compiler/rustc_data_structures" }
rustc_errors = { path = "/home/matthias/vcs/github/rust/compiler/rustc_errors" }
rustc_hir = { path = "/home/matthias/vcs/github/rust/compiler/rustc_hir" }
rustc_hir_pretty = { path = "/home/matthias/vcs/github/rust/compiler/rustc_hir_pretty" }
rustc_index = { path = "/home/matthias/vcs/github/rust/compiler/rustc_index" }
rustc_infer = { path = "/home/matthias/vcs/github/rust/compiler/rustc_infer" }
rustc_lexer = { path = "/home/matthias/vcs/github/rust/compiler/rustc_lexer" }
rustc_lint = { path = "/home/matthias/vcs/github/rust/compiler/rustc_lint" }
rustc_middle = { path = "/home/matthias/vcs/github/rust/compiler/rustc_middle" }
rustc_mir = { path = "/home/matthias/vcs/github/rust/compiler/rustc_mir" }
rustc_parse = { path = "/home/matthias/vcs/github/rust/compiler/rustc_parse" }
rustc_parse_format = { path = "/home/matthias/vcs/github/rust/compiler/rustc_parse_format" }
rustc_session = { path = "/home/matthias/vcs/github/rust/compiler/rustc_session" }
rustc_span = { path = "/home/matthias/vcs/github/rust/compiler/rustc_span" }
rustc_target = { path = "/home/matthias/vcs/github/rust/compiler/rustc_target" }
rustc_trait_selection = { path = "/home/matthias/vcs/github/rust/compiler/rustc_trait_selection" }
rustc_typeck = { path = "/home/matthias/vcs/github/rust/compiler/rustc_typeck" }

[dependencies]
cargo_metadata = "0.12"
if_chain = "1.0.0"
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/needless_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
return;
}
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, ref inner) = e.kind {
if let ty::Ref(..) = cx.typeck_results().expr_ty(inner).kind() {
if let ty::Ref(_,ty,_) = cx.typeck_results().expr_ty(inner).kind() {
for adj3 in cx.typeck_results().expr_adjustments(e).windows(3) {
if let [Adjustment {
kind: Adjust::Deref(_), ..
Expand All @@ -62,8 +62,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
cx,
NEEDLESS_BORROW,
e.span,
"this expression borrows a reference that is immediately dereferenced \
by the compiler",
&format!("this expression borrows a reference (`&{}`) that is immediately dereferenced \
by the compiler", ty),
|diag| {
if let Some(snippet) = snippet_opt(cx, inner.span) {
diag.span_suggestion(
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/eta.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ error: redundant closure found
LL | meta(|a| foo(a));
| ^^^^^^^^^^ help: remove closure as shown: `foo`

error: this expression borrows a reference that is immediately dereferenced by the compiler
error: this expression borrows a reference (`&u8`) that is immediately dereferenced by the compiler
--> $DIR/eta.rs:24:21
|
LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/needless_borrow.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: this expression borrows a reference that is immediately dereferenced by the compiler
error: this expression borrows a reference (`&i32`) that is immediately dereferenced by the compiler
--> $DIR/needless_borrow.rs:14:15
|
LL | let c = x(&&a);
Expand All @@ -12,7 +12,7 @@ error: this pattern creates a reference to a reference
LL | if let Some(ref cake) = Some(&5) {}
| ^^^^^^^^ help: change this to: `cake`

error: this expression borrows a reference that is immediately dereferenced by the compiler
error: this expression borrows a reference (`&i32`) that is immediately dereferenced by the compiler
--> $DIR/needless_borrow.rs:28:15
|
LL | 46 => &&a,
Expand Down

0 comments on commit 144ee13

Please sign in to comment.