Skip to content

lint idea: useless_default_generic_parameters #14848

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

Open
y86-dev opened this issue May 19, 2025 · 8 comments
Open

lint idea: useless_default_generic_parameters #14848

y86-dev opened this issue May 19, 2025 · 8 comments
Labels
A-lint Area: New lints good first issue These issues are a good way to get started with Clippy

Comments

@y86-dev
Copy link

y86-dev commented May 19, 2025

What it does

Given a type with a generic argument that has a default:

type Result<T = ()> = core::result::Result<T, MyError>;

Usage of this type when specifying the default should not use generics:

fn foo() -> Result<()> { Ok(()) }
//                ^^^^ unnecessary generic, `()` already is the default
//                hint: use `Result` instead.

Advantage

  • Removes duplication of the default value
  • Reduces visual clutter
  • Reminds people that the type has a default value

Drawbacks

  • when a library adds a default value, one gets this warning when updating
  • macros might trigger this involuntarily

Example

type Result<T = ()> = core::result::Result<T, MyError>;

fn foo() -> Result<()> {
    Ok(())
}

Could be written as:

type Result<T = ()> = core::result::Result<T, MyError>;

fn foo() -> Result {
    Ok(())
}
@y86-dev y86-dev added the A-lint Area: New lints label May 19, 2025
@dakr
Copy link

dakr commented May 19, 2025

+1, I think that's a good idea.

@samueltardieu samueltardieu added the good first issue These issues are a good way to get started with Clippy label May 19, 2025
@y86-dev
Copy link
Author

y86-dev commented May 19, 2025

I'm not 100% satisfied with the name of the lint "unnecessary_generic", but I also couldn't come up with something better.

@ojeda
Copy link
Contributor

ojeda commented May 19, 2025

+1, we had this as Rust-for-Linux/linux#1128 as a kernel-specific checkpath.pl "lint", but if Clippy can do this, that would way better: it would cover more projects and more generic types.

@samueltardieu
Copy link
Contributor

It has to work with allow/deny sentences, such as in "allow useless default parameters", so useless_default_parameters or something like that might be an option.

@y86-dev
Copy link
Author

y86-dev commented May 19, 2025

+1, we had this as Rust-for-Linux/linux#1128 as a kernel-specific checkpath.pl "lint", but if Clippy can do this, that would way better: it would cover more projects and more generic types.

Ah I see, I didn't check, since I think putting this in clippy is the right way, there is nothing kernel-specific or obscure about this lint :)

@y86-dev y86-dev changed the title lint idea: unnecessary_generic lint idea: useless_default_parameters May 19, 2025
@y86-dev
Copy link
Author

y86-dev commented May 19, 2025

It has to work with allow/deny sentences, such as in "allow useless default parameters", so useless_default_parameters or something like that might be an option.

that sounds much better :)

@y86-dev y86-dev changed the title lint idea: useless_default_parameters lint idea: useless_default_generic_parameters May 19, 2025
@ojeda
Copy link
Contributor

ojeda commented May 19, 2025

Ah I see, I didn't check, since I think putting this in clippy is the right way, there is nothing kernel-specific or obscure about this lint :)

Yeah, definitely.

(To clarify, they are not exclusive, i.e. ideally it would be faster to add a checkpatch.pl, which applies to all Rust versions and may help justify a Clippy lint, but sadly I don't think there is much bandwidth on the checkpatch.pl side).

@Jarcho
Copy link
Contributor

Jarcho commented May 22, 2025

explicit_default_arguments might be better. The implementation has to avoid linting anywhere type inference happens so check_ty can't be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good first issue These issues are a good way to get started with Clippy
Projects
None yet
Development

No branches or pull requests

5 participants