Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[new_without_default]: include where clause in suggestions, make applicable #11280

Merged
merged 3 commits into from
Aug 18, 2023
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
21 changes: 17 additions & 4 deletions clippy_lints/src/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
}

let generics_sugg = snippet(cx, generics.span, "");
let where_clause_sugg = if generics.has_where_clause_predicates {
format!("\n{}\n", snippet(cx, generics.where_clause_span, ""))
} else {
String::new()
};
let self_ty_fmt = self_ty.to_string();
let self_type_snip = snippet(cx, impl_self_ty.span, &self_ty_fmt);
span_lint_hir_and_then(
Expand All @@ -145,8 +150,12 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
cx,
item.span,
"try adding this",
&create_new_without_default_suggest_msg(&self_type_snip, &generics_sugg),
Applicability::MaybeIncorrect,
&create_new_without_default_suggest_msg(
&self_type_snip,
&generics_sugg,
&where_clause_sugg
),
Applicability::MachineApplicable,
);
},
);
Expand All @@ -159,10 +168,14 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
}
}

fn create_new_without_default_suggest_msg(self_type_snip: &str, generics_sugg: &str) -> String {
fn create_new_without_default_suggest_msg(
self_type_snip: &str,
generics_sugg: &str,
where_clause_sugg: &str,
) -> String {
#[rustfmt::skip]
format!(
"impl{generics_sugg} Default for {self_type_snip} {{
"impl{generics_sugg} Default for {self_type_snip}{where_clause_sugg} {{
fn default() -> Self {{
Self::new()
}}
Expand Down
28 changes: 27 additions & 1 deletion tests/ui/new_without_default.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl<'c> LtKo<'c> {
pub fn new() -> LtKo<'c> {
unimplemented!()
}
// FIXME: that suggestion is missing lifetimes
}

struct Private;
Expand Down Expand Up @@ -273,3 +272,30 @@ impl IgnoreLifetimeNew {
Self
}
}

// From issue #11267

pub struct MyStruct<K, V>
where
K: std::hash::Hash + Eq + PartialEq,
{
_kv: Option<(K, V)>,
}

impl<K, V> Default for MyStruct<K, V>
where
K: std::hash::Hash + Eq + PartialEq,
{
fn default() -> Self {
Self::new()
}
}

impl<K, V> MyStruct<K, V>
where
K: std::hash::Hash + Eq + PartialEq,
{
pub fn new() -> Self {
Self { _kv: None }
}
}
19 changes: 18 additions & 1 deletion tests/ui/new_without_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ impl<'c> LtKo<'c> {
pub fn new() -> LtKo<'c> {
unimplemented!()
}
// FIXME: that suggestion is missing lifetimes
}

struct Private;
Expand Down Expand Up @@ -231,3 +230,21 @@ impl IgnoreLifetimeNew {
Self
}
}

// From issue #11267

pub struct MyStruct<K, V>
where
K: std::hash::Hash + Eq + PartialEq,
{
_kv: Option<(K, V)>,
}

impl<K, V> MyStruct<K, V>
where
K: std::hash::Hash + Eq + PartialEq,
{
pub fn new() -> Self {
Self { _kv: None }
}
}
30 changes: 25 additions & 5 deletions tests/ui/new_without_default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ LL + }
|

error: you should consider adding a `Default` implementation for `NewNotEqualToDerive`
--> $DIR/new_without_default.rs:177:5
--> $DIR/new_without_default.rs:176:5
|
LL | / pub fn new() -> Self {
LL | | NewNotEqualToDerive { foo: 1 }
Expand All @@ -68,7 +68,7 @@ LL + }
|

error: you should consider adding a `Default` implementation for `FooGenerics<T>`
--> $DIR/new_without_default.rs:185:5
--> $DIR/new_without_default.rs:184:5
|
LL | / pub fn new() -> Self {
LL | | Self(Default::default())
Expand All @@ -85,7 +85,7 @@ LL + }
|

error: you should consider adding a `Default` implementation for `BarGenerics<T>`
--> $DIR/new_without_default.rs:192:5
--> $DIR/new_without_default.rs:191:5
|
LL | / pub fn new() -> Self {
LL | | Self(Default::default())
Expand All @@ -102,7 +102,7 @@ LL + }
|

error: you should consider adding a `Default` implementation for `Foo<T>`
--> $DIR/new_without_default.rs:203:9
--> $DIR/new_without_default.rs:202:9
|
LL | / pub fn new() -> Self {
LL | | todo!()
Expand All @@ -120,5 +120,25 @@ LL +
LL ~ impl<T> Foo<T> {
|

error: aborting due to 7 previous errors
error: you should consider adding a `Default` implementation for `MyStruct<K, V>`
--> $DIR/new_without_default.rs:247:5
|
LL | / pub fn new() -> Self {
LL | | Self { _kv: None }
LL | | }
| |_____^
|
help: try adding this
|
LL + impl<K, V> Default for MyStruct<K, V>
LL + where
LL + K: std::hash::Hash + Eq + PartialEq,
LL + {
LL + fn default() -> Self {
LL + Self::new()
LL + }
LL + }
|

error: aborting due to 8 previous errors