Skip to content

Commit 3f48ed5

Browse files
committed
Auto merge of rust-lang#10160 - llogiq:default-trim-paths, r=dswij
trim paths in `default_trait_access`/`clone_on_copy` suggestions This should help making the suggestions more palatable. Similar to rust-lang#10153. --- changelog: trim paths in [`default_trait_access`]/[`clone_on_copy`] suggestions
2 parents 3816f9a + 05ba519 commit 3f48ed5

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

clippy_lints/src/default.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir::def::Res;
1111
use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
1212
use rustc_lint::{LateContext, LateLintPass};
1313
use rustc_middle::ty;
14+
use rustc_middle::ty::print::with_forced_trimmed_paths;
1415
use rustc_session::{declare_tool_lint, impl_lint_pass};
1516
use rustc_span::symbol::{Ident, Symbol};
1617
use rustc_span::Span;
@@ -98,9 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
9899
if let ty::Adt(def, ..) = expr_ty.kind();
99100
if !is_from_proc_macro(cx, expr);
100101
then {
101-
// TODO: Work out a way to put "whatever the imported way of referencing
102-
// this type in this file" rather than a fully-qualified type.
103-
let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did()));
102+
let replacement = with_forced_trimmed_paths!(format!("{}::default()", cx.tcx.def_path_str(def.did())));
104103
span_lint_and_sugg(
105104
cx,
106105
DEFAULT_TRAIT_ACCESS,

clippy_lints/src/methods/clone_on_copy.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clippy_utils::ty::is_copy;
66
use rustc_errors::Applicability;
77
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
88
use rustc_lint::LateContext;
9-
use rustc_middle::ty::{self, adjustment::Adjust};
9+
use rustc_middle::ty::{self, adjustment::Adjust, print::with_forced_trimmed_paths};
1010
use rustc_span::symbol::{sym, Symbol};
1111

1212
use super::CLONE_DOUBLE_REF;
@@ -47,10 +47,10 @@ pub(super) fn check(
4747
cx,
4848
CLONE_DOUBLE_REF,
4949
expr.span,
50-
&format!(
50+
&with_forced_trimmed_paths!(format!(
5151
"using `clone` on a double-reference; \
5252
this will copy the reference of type `{ty}` instead of cloning the inner type"
53-
),
53+
)),
5454
|diag| {
5555
if let Some(snip) = sugg::Sugg::hir_opt(cx, arg) {
5656
let mut ty = innermost;
@@ -61,11 +61,11 @@ pub(super) fn check(
6161
}
6262
let refs = "&".repeat(n + 1);
6363
let derefs = "*".repeat(n);
64-
let explicit = format!("<{refs}{ty}>::clone({snip})");
64+
let explicit = with_forced_trimmed_paths!(format!("<{refs}{ty}>::clone({snip})"));
6565
diag.span_suggestion(
6666
expr.span,
6767
"try dereferencing it",
68-
format!("{refs}({derefs}{}).clone()", snip.deref()),
68+
with_forced_trimmed_paths!(format!("{refs}({derefs}{}).clone()", snip.deref())),
6969
Applicability::MaybeIncorrect,
7070
);
7171
diag.span_suggestion(
@@ -129,7 +129,9 @@ pub(super) fn check(
129129
cx,
130130
CLONE_ON_COPY,
131131
expr.span,
132-
&format!("using `clone` on type `{ty}` which implements the `Copy` trait"),
132+
&with_forced_trimmed_paths!(format!(
133+
"using `clone` on type `{ty}` which implements the `Copy` trait"
134+
)),
133135
help,
134136
sugg,
135137
app,

tests/ui/clone_on_copy.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ error: using `clone` on type `i32` which implements the `Copy` trait
4848
LL | vec.push(42.clone());
4949
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
5050

51-
error: using `clone` on type `std::option::Option<i32>` which implements the `Copy` trait
51+
error: using `clone` on type `Option<i32>` which implements the `Copy` trait
5252
--> $DIR/clone_on_copy.rs:77:17
5353
|
5454
LL | let value = opt.clone()?; // operator precedence needed (*opt)?

tests/ui/default_trait_access.fixed

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ use std::default::Default as D2;
1212
use std::string;
1313

1414
fn main() {
15-
let s1: String = std::string::String::default();
15+
let s1: String = String::default();
1616

1717
let s2 = String::default();
1818

19-
let s3: String = std::string::String::default();
19+
let s3: String = String::default();
2020

21-
let s4: String = std::string::String::default();
21+
let s4: String = String::default();
2222

2323
let s5 = string::String::default();
2424

25-
let s6: String = std::string::String::default();
25+
let s6: String = String::default();
2626

2727
let s7 = std::string::String::default();
2828

tests/ui/default_trait_access.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
error: calling `std::string::String::default()` is more clear than this expression
1+
error: calling `String::default()` is more clear than this expression
22
--> $DIR/default_trait_access.rs:15:22
33
|
44
LL | let s1: String = Default::default();
5-
| ^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
5+
| ^^^^^^^^^^^^^^^^^^ help: try: `String::default()`
66
|
77
note: the lint level is defined here
88
--> $DIR/default_trait_access.rs:3:9
99
|
1010
LL | #![deny(clippy::default_trait_access)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: calling `std::string::String::default()` is more clear than this expression
13+
error: calling `String::default()` is more clear than this expression
1414
--> $DIR/default_trait_access.rs:19:22
1515
|
1616
LL | let s3: String = D2::default();
17-
| ^^^^^^^^^^^^^ help: try: `std::string::String::default()`
17+
| ^^^^^^^^^^^^^ help: try: `String::default()`
1818

19-
error: calling `std::string::String::default()` is more clear than this expression
19+
error: calling `String::default()` is more clear than this expression
2020
--> $DIR/default_trait_access.rs:21:22
2121
|
2222
LL | let s4: String = std::default::Default::default();
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `String::default()`
2424

25-
error: calling `std::string::String::default()` is more clear than this expression
25+
error: calling `String::default()` is more clear than this expression
2626
--> $DIR/default_trait_access.rs:25:22
2727
|
2828
LL | let s6: String = default::Default::default();
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `String::default()`
3030

3131
error: calling `GenericDerivedDefault::default()` is more clear than this expression
3232
--> $DIR/default_trait_access.rs:35:46

tests/ui/unnecessary_clone.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ LL | t.clone();
3838
|
3939
= note: `-D clippy::clone-on-copy` implied by `-D warnings`
4040

41-
error: using `clone` on type `std::option::Option<T>` which implements the `Copy` trait
41+
error: using `clone` on type `Option<T>` which implements the `Copy` trait
4242
--> $DIR/unnecessary_clone.rs:42:5
4343
|
4444
LL | Some(t).clone();
4545
| ^^^^^^^^^^^^^^^ help: try removing the `clone` call: `Some(t)`
4646

47-
error: using `clone` on a double-reference; this will copy the reference of type `&std::vec::Vec<i32>` instead of cloning the inner type
47+
error: using `clone` on a double-reference; this will copy the reference of type `&Vec<i32>` instead of cloning the inner type
4848
--> $DIR/unnecessary_clone.rs:48:22
4949
|
5050
LL | let z: &Vec<_> = y.clone();
@@ -57,10 +57,10 @@ LL | let z: &Vec<_> = &(*y).clone();
5757
| ~~~~~~~~~~~~~
5858
help: or try being explicit if you are sure, that you want to clone a reference
5959
|
60-
LL | let z: &Vec<_> = <&std::vec::Vec<i32>>::clone(y);
61-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60+
LL | let z: &Vec<_> = <&Vec<i32>>::clone(y);
61+
| ~~~~~~~~~~~~~~~~~~~~~
6262

63-
error: using `clone` on type `many_derefs::E` which implements the `Copy` trait
63+
error: using `clone` on type `E` which implements the `Copy` trait
6464
--> $DIR/unnecessary_clone.rs:84:20
6565
|
6666
LL | let _: E = a.clone();

0 commit comments

Comments
 (0)