mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
trim paths in default_trait_access
/clone_on_copy
suggestions
This commit is contained in:
parent
0bca8dd254
commit
05ba519a3a
6 changed files with 28 additions and 27 deletions
|
@ -11,6 +11,7 @@ use rustc_hir::def::Res;
|
||||||
use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
|
use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
|
use rustc_middle::ty::print::with_forced_trimmed_paths;
|
||||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||||
use rustc_span::symbol::{Ident, Symbol};
|
use rustc_span::symbol::{Ident, Symbol};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
@ -98,9 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
|
||||||
if let ty::Adt(def, ..) = expr_ty.kind();
|
if let ty::Adt(def, ..) = expr_ty.kind();
|
||||||
if !is_from_proc_macro(cx, expr);
|
if !is_from_proc_macro(cx, expr);
|
||||||
then {
|
then {
|
||||||
// TODO: Work out a way to put "whatever the imported way of referencing
|
let replacement = with_forced_trimmed_paths!(format!("{}::default()", cx.tcx.def_path_str(def.did())));
|
||||||
// this type in this file" rather than a fully-qualified type.
|
|
||||||
let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did()));
|
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
DEFAULT_TRAIT_ACCESS,
|
DEFAULT_TRAIT_ACCESS,
|
||||||
|
|
|
@ -6,7 +6,7 @@ use clippy_utils::ty::is_copy;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
|
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_middle::ty::{self, adjustment::Adjust};
|
use rustc_middle::ty::{self, adjustment::Adjust, print::with_forced_trimmed_paths};
|
||||||
use rustc_span::symbol::{sym, Symbol};
|
use rustc_span::symbol::{sym, Symbol};
|
||||||
|
|
||||||
use super::CLONE_DOUBLE_REF;
|
use super::CLONE_DOUBLE_REF;
|
||||||
|
@ -47,10 +47,10 @@ pub(super) fn check(
|
||||||
cx,
|
cx,
|
||||||
CLONE_DOUBLE_REF,
|
CLONE_DOUBLE_REF,
|
||||||
expr.span,
|
expr.span,
|
||||||
&format!(
|
&with_forced_trimmed_paths!(format!(
|
||||||
"using `clone` on a double-reference; \
|
"using `clone` on a double-reference; \
|
||||||
this will copy the reference of type `{ty}` instead of cloning the inner type"
|
this will copy the reference of type `{ty}` instead of cloning the inner type"
|
||||||
),
|
)),
|
||||||
|diag| {
|
|diag| {
|
||||||
if let Some(snip) = sugg::Sugg::hir_opt(cx, arg) {
|
if let Some(snip) = sugg::Sugg::hir_opt(cx, arg) {
|
||||||
let mut ty = innermost;
|
let mut ty = innermost;
|
||||||
|
@ -61,11 +61,11 @@ pub(super) fn check(
|
||||||
}
|
}
|
||||||
let refs = "&".repeat(n + 1);
|
let refs = "&".repeat(n + 1);
|
||||||
let derefs = "*".repeat(n);
|
let derefs = "*".repeat(n);
|
||||||
let explicit = format!("<{refs}{ty}>::clone({snip})");
|
let explicit = with_forced_trimmed_paths!(format!("<{refs}{ty}>::clone({snip})"));
|
||||||
diag.span_suggestion(
|
diag.span_suggestion(
|
||||||
expr.span,
|
expr.span,
|
||||||
"try dereferencing it",
|
"try dereferencing it",
|
||||||
format!("{refs}({derefs}{}).clone()", snip.deref()),
|
with_forced_trimmed_paths!(format!("{refs}({derefs}{}).clone()", snip.deref())),
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
diag.span_suggestion(
|
diag.span_suggestion(
|
||||||
|
@ -129,7 +129,9 @@ pub(super) fn check(
|
||||||
cx,
|
cx,
|
||||||
CLONE_ON_COPY,
|
CLONE_ON_COPY,
|
||||||
expr.span,
|
expr.span,
|
||||||
&format!("using `clone` on type `{ty}` which implements the `Copy` trait"),
|
&with_forced_trimmed_paths!(format!(
|
||||||
|
"using `clone` on type `{ty}` which implements the `Copy` trait"
|
||||||
|
)),
|
||||||
help,
|
help,
|
||||||
sugg,
|
sugg,
|
||||||
app,
|
app,
|
||||||
|
|
|
@ -48,7 +48,7 @@ error: using `clone` on type `i32` which implements the `Copy` trait
|
||||||
LL | vec.push(42.clone());
|
LL | vec.push(42.clone());
|
||||||
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
|
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
|
||||||
|
|
||||||
error: using `clone` on type `std::option::Option<i32>` which implements the `Copy` trait
|
error: using `clone` on type `Option<i32>` which implements the `Copy` trait
|
||||||
--> $DIR/clone_on_copy.rs:77:17
|
--> $DIR/clone_on_copy.rs:77:17
|
||||||
|
|
|
|
||||||
LL | let value = opt.clone()?; // operator precedence needed (*opt)?
|
LL | let value = opt.clone()?; // operator precedence needed (*opt)?
|
||||||
|
|
|
@ -12,17 +12,17 @@ use std::default::Default as D2;
|
||||||
use std::string;
|
use std::string;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let s1: String = std::string::String::default();
|
let s1: String = String::default();
|
||||||
|
|
||||||
let s2 = String::default();
|
let s2 = String::default();
|
||||||
|
|
||||||
let s3: String = std::string::String::default();
|
let s3: String = String::default();
|
||||||
|
|
||||||
let s4: String = std::string::String::default();
|
let s4: String = String::default();
|
||||||
|
|
||||||
let s5 = string::String::default();
|
let s5 = string::String::default();
|
||||||
|
|
||||||
let s6: String = std::string::String::default();
|
let s6: String = String::default();
|
||||||
|
|
||||||
let s7 = std::string::String::default();
|
let s7 = std::string::String::default();
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
error: calling `std::string::String::default()` is more clear than this expression
|
error: calling `String::default()` is more clear than this expression
|
||||||
--> $DIR/default_trait_access.rs:15:22
|
--> $DIR/default_trait_access.rs:15:22
|
||||||
|
|
|
|
||||||
LL | let s1: String = Default::default();
|
LL | let s1: String = Default::default();
|
||||||
| ^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
|
| ^^^^^^^^^^^^^^^^^^ help: try: `String::default()`
|
||||||
|
|
|
|
||||||
note: the lint level is defined here
|
note: the lint level is defined here
|
||||||
--> $DIR/default_trait_access.rs:3:9
|
--> $DIR/default_trait_access.rs:3:9
|
||||||
|
@ -10,23 +10,23 @@ note: the lint level is defined here
|
||||||
LL | #![deny(clippy::default_trait_access)]
|
LL | #![deny(clippy::default_trait_access)]
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: calling `std::string::String::default()` is more clear than this expression
|
error: calling `String::default()` is more clear than this expression
|
||||||
--> $DIR/default_trait_access.rs:19:22
|
--> $DIR/default_trait_access.rs:19:22
|
||||||
|
|
|
|
||||||
LL | let s3: String = D2::default();
|
LL | let s3: String = D2::default();
|
||||||
| ^^^^^^^^^^^^^ help: try: `std::string::String::default()`
|
| ^^^^^^^^^^^^^ help: try: `String::default()`
|
||||||
|
|
||||||
error: calling `std::string::String::default()` is more clear than this expression
|
error: calling `String::default()` is more clear than this expression
|
||||||
--> $DIR/default_trait_access.rs:21:22
|
--> $DIR/default_trait_access.rs:21:22
|
||||||
|
|
|
|
||||||
LL | let s4: String = std::default::Default::default();
|
LL | let s4: String = std::default::Default::default();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `String::default()`
|
||||||
|
|
||||||
error: calling `std::string::String::default()` is more clear than this expression
|
error: calling `String::default()` is more clear than this expression
|
||||||
--> $DIR/default_trait_access.rs:25:22
|
--> $DIR/default_trait_access.rs:25:22
|
||||||
|
|
|
|
||||||
LL | let s6: String = default::Default::default();
|
LL | let s6: String = default::Default::default();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `String::default()`
|
||||||
|
|
||||||
error: calling `GenericDerivedDefault::default()` is more clear than this expression
|
error: calling `GenericDerivedDefault::default()` is more clear than this expression
|
||||||
--> $DIR/default_trait_access.rs:35:46
|
--> $DIR/default_trait_access.rs:35:46
|
||||||
|
|
|
@ -38,13 +38,13 @@ LL | t.clone();
|
||||||
|
|
|
|
||||||
= note: `-D clippy::clone-on-copy` implied by `-D warnings`
|
= note: `-D clippy::clone-on-copy` implied by `-D warnings`
|
||||||
|
|
||||||
error: using `clone` on type `std::option::Option<T>` which implements the `Copy` trait
|
error: using `clone` on type `Option<T>` which implements the `Copy` trait
|
||||||
--> $DIR/unnecessary_clone.rs:42:5
|
--> $DIR/unnecessary_clone.rs:42:5
|
||||||
|
|
|
|
||||||
LL | Some(t).clone();
|
LL | Some(t).clone();
|
||||||
| ^^^^^^^^^^^^^^^ help: try removing the `clone` call: `Some(t)`
|
| ^^^^^^^^^^^^^^^ help: try removing the `clone` call: `Some(t)`
|
||||||
|
|
||||||
error: using `clone` on a double-reference; this will copy the reference of type `&std::vec::Vec<i32>` instead of cloning the inner type
|
error: using `clone` on a double-reference; this will copy the reference of type `&Vec<i32>` instead of cloning the inner type
|
||||||
--> $DIR/unnecessary_clone.rs:48:22
|
--> $DIR/unnecessary_clone.rs:48:22
|
||||||
|
|
|
|
||||||
LL | let z: &Vec<_> = y.clone();
|
LL | let z: &Vec<_> = y.clone();
|
||||||
|
@ -57,10 +57,10 @@ LL | let z: &Vec<_> = &(*y).clone();
|
||||||
| ~~~~~~~~~~~~~
|
| ~~~~~~~~~~~~~
|
||||||
help: or try being explicit if you are sure, that you want to clone a reference
|
help: or try being explicit if you are sure, that you want to clone a reference
|
||||||
|
|
|
|
||||||
LL | let z: &Vec<_> = <&std::vec::Vec<i32>>::clone(y);
|
LL | let z: &Vec<_> = <&Vec<i32>>::clone(y);
|
||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
error: using `clone` on type `many_derefs::E` which implements the `Copy` trait
|
error: using `clone` on type `E` which implements the `Copy` trait
|
||||||
--> $DIR/unnecessary_clone.rs:84:20
|
--> $DIR/unnecessary_clone.rs:84:20
|
||||||
|
|
|
|
||||||
LL | let _: E = a.clone();
|
LL | let _: E = a.clone();
|
||||||
|
|
Loading…
Reference in a new issue