-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Correctly handle bracketed type in default_constructed_unit_struct
#14367
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
Correctly handle bracketed type in default_constructed_unit_struct
#14367
Conversation
An expression such as `<_>::default()` should not be replaced by an ambiguous type name such as `_` even if the inferred type in the original expression is a singleton.
This is more consistent with what other lints are doing: the use of `default` to create a unit struct is the whole expression.
r? clippy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, r=me with the dbg removed
{ | ||
span_lint_and_sugg( | ||
let mut removals = vec![(expr.span.with_lo(qpath.qself_span().hi()), String::new())]; | ||
if expr.span.with_source_text(cx, |s| dbg!(s).starts_with('<')) == Some(true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you forgot to remove a dbg
here (maybe we should run clippy_lints dogfood with -Dclippy::dbg_macro
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this! I've submitted #14579 to avoid this in the future.
253d6eb
to
ffe7f20
Compare
When `<T>::default()` is replaced by `T` when `T` is a singleton, the brackets around the type name can be removed.
ffe7f20
to
0aa0d07
Compare
There were two bugs here. Let's assume
T
is a singleton type implementingDefault
and thatf()
takes aT
:f(<T>::default())
cannot be replaced byf(<T)
as it was (incorrect spans – this is tricky because the type relative path uses a base span covering onlyT
, not<T>
) (third commit)f(<_>::default())
is inferred correctly, but cannot be replaced by<_>
or_
, as this cannot be used to infer an instance of a singleton type (first commit).The second commit offers better error messages by pointing at the whole expression.
Fix #12654
changelog: [
default_constructed_unit_struct
]: do not suggest incorrect fix when using a type surrounded by brackets