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 lint [trivial_default_constructed_types] #10985

Closed

Conversation

Centri3
Copy link
Member

@Centri3 Centri3 commented Jun 18, 2023

Will lint on cases like <&str>::default or u32::default, etc.

changelog: New lint [trivial_default_constructed_types]

@rustbot
Copy link
Collaborator

rustbot commented Jun 18, 2023

r? @Jarcho

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 18, 2023
@Centri3 Centri3 force-pushed the trivial_default_constructed_types branch from ad0ed39 to fa1c3c8 Compare June 18, 2023 18:56
@bors
Copy link
Collaborator

bors commented Jun 19, 2023

☔ The latest upstream changes (presumably #10951) made this pull request unmergeable. Please resolve the merge conflicts.

@Centri3 Centri3 force-pushed the trivial_default_constructed_types branch from fa1c3c8 to f1b4fbc Compare June 19, 2023 03:38
@bors
Copy link
Collaborator

bors commented Jun 27, 2023

☔ The latest upstream changes (presumably #10884) made this pull request unmergeable. Please resolve the merge conflicts.

@Centri3 Centri3 force-pushed the trivial_default_constructed_types branch 2 times, most recently from 9fdd6a1 to 834f089 Compare June 27, 2023 21:15
@Centri3 Centri3 force-pushed the trivial_default_constructed_types branch 2 times, most recently from 5a9b2be to dcaf1b1 Compare June 29, 2023 10:01
fn default_value(cx: &LateContext<'_>, ty: Ty<'_>) -> Option<Cow<'static, str>> {
match ty.kind() {
ty::Adt(def, substs) if let [subst] = substs.as_slice() => {
is_lang_item_or_ctor(cx, def.did(), LangItem::Option).then(|| format!("None::<{subst}>").into())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the type argument to None when it's not needed isn't really an improvement. None::<u32> is ok. None::<Vec<String>> is not great. None::<Arc<Mutex<SomeStruct<String>>>> is terrible. None of them are better than Option::default().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would likely need to use the QPath instead of Ty to fix that, but this would ignore type aliases and the like

Copy link
Member Author

@Centri3 Centri3 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the easiest way to "fix" this is to just make it HasPlaceholders in the None case, and use ::<_>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You bring up a fun issue with type aliases. Numeric literals can't just use type suffixes if the required type is an alias since it could be configuration dependent.

With all the edge cases I don't think there's any way to avoid parsing the type part of the path. Only linting when either the type is missing (e.g. Default::default()), or the type is a single identifier matching the expected type's name (e.g. Option::default()) is fine. If somebody does something stupid like type u32 = i32; then the lint can not trigger. type MyInt = i32; is probably better not linted as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, so parsing the path is necessary then. I believe we can use .into() for type aliases in particular, or <name>::from(x) perhaps (which is usually what I use for creating type aliases). But we probably shouldn't lint them yeah

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is easier to do now with expr_use_ctxt. You'll have to add a function to get the defined type as a HIR type, but that's mostly copying the existing function. This won't work for anything defined in another crate unfortunately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I feel like at this point it'd just be easier to store where it's a type alias directly in the Ty's info 😅 It can probably be changed upstream rather easily

I may look into that...

Copy link
Contributor

@Jarcho Jarcho Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to take type inference into account, so you would still have to look up the type as it was written either way.

Copy link
Member Author

@Centri3 Centri3 Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, but at least we'd be able to ignore type aliases more easily. Not a big deal for u32::default where u32 is type u32 = i32 but Default::default is currently pretty annoying (same with stuff like function calls outside the current crate)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, for now, I'll change this to lint type aliases for Default::default and ignoring them on u32::default using the Res. One day, once lazy_type_alias is stable (and hopefully that normalization is delayed until after typeck 😅) then we can ignore them entirely.

@bors
Copy link
Collaborator

bors commented Jul 1, 2023

☔ The latest upstream changes (presumably #11020) made this pull request unmergeable. Please resolve the merge conflicts.

@Centri3 Centri3 force-pushed the trivial_default_constructed_types branch from 7aee58b to d8ec7df Compare July 1, 2023 17:34
@bors
Copy link
Collaborator

bors commented Jul 8, 2023

☔ The latest upstream changes (presumably #10788) made this pull request unmergeable. Please resolve the merge conflicts.

@Centri3 Centri3 force-pushed the trivial_default_constructed_types branch from d8ec7df to 38731ab Compare July 10, 2023 04:01
@bors
Copy link
Collaborator

bors commented Jul 18, 2023

☔ The latest upstream changes (presumably #11140) made this pull request unmergeable. Please resolve the merge conflicts.

@Centri3 Centri3 force-pushed the trivial_default_constructed_types branch from 38731ab to 5f49b56 Compare August 5, 2023 19:15
@xFrednet
Copy link
Member

Hey this is triage, I'm closing this due to inactivity. Currently, @Centri3 sadly doesn't have the time to continue this implementation. If anyone is interested in continuing this PR, you're more than welcome to create a new PR and push it over the finish line. :D

Thank you to @Centri3 and the reviewers for the time, that you already put into this!

@rustbot label +S-inactive-closed -S-waiting-on-author -S-waiting-on-review

@xFrednet xFrednet closed this Mar 31, 2024
@rustbot rustbot added S-inactive-closed Status: Closed due to inactivity and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 31, 2024
@Centri3 Centri3 deleted the trivial_default_constructed_types branch April 1, 2024 16:24
@Centri3
Copy link
Member Author

Centri3 commented Apr 1, 2024

Triaging some of my PRs in case someone wants to pick them up, this one is pretty close to being done I think. The ergonomics are good, It doesn't mess with anything it knows it can't change, and also tries to omit generic parameters, etc.

Mostly, I think we should extract this weird HIR + Ty abstraction I conjured and maybe also wait until lazy_type_alias is stabilized (if it is), or just live with the FPs - I feel like they wouldn't be that common, similar to other lints that operate on Ty.

If nobody else does, I'll definitely finish this at some point in the future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive-closed Status: Closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants