Skip to content

Add explicit_default_arguments lint#16123

Open
vogelbirb wants to merge 14 commits intorust-lang:masterfrom
vogelbirb:master
Open

Add explicit_default_arguments lint#16123
vogelbirb wants to merge 14 commits intorust-lang:masterfrom
vogelbirb:master

Conversation

@vogelbirb
Copy link
Copy Markdown

@vogelbirb vogelbirb commented Nov 21, 2025

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Nov 21, 2025
@rustbot

This comment has been minimized.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Dec 29, 2025

Lintcheck changes for 90ff88e

Lint Added Removed Changed
clippy::explicit_default_arguments 9 0 0

This comment will be updated if you push new changes

@vogelbirb vogelbirb marked this pull request as ready for review December 29, 2025 06:51
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 29, 2025
@vogelbirb
Copy link
Copy Markdown
Author

Do you think the function walk_ty_recursive and some of its helper functions are worth moving to clippy_utils? I'm not too sure how that works.

@vogelbirb vogelbirb changed the title Add explicit_default_arguments lint Add explicit_default_arguments lint Dec 29, 2025
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 21, 2026
@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) has-merge-commits PR has merge commits, merge with caution. labels Jan 21, 2026
@vogelbirb
Copy link
Copy Markdown
Author

Hi, @Jarcho. I just wanted to follow up on this PR, it's ready for review and has been waiting for a few weeks now. I took into account your advice on Zulip. It would be great if you could take a look at it. Thanks.

@rustbot

This comment has been minimized.

Matheus L. P. added 9 commits March 23, 2026 18:04
Most notably, `ExprKind::Path` is still a work in progress. Also added a check for equality constraints in `impl Trait`
and trait objects. Unlike consts, statics were missing so a check for them was added.
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 23, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@pitaj
Copy link
Copy Markdown
Contributor

pitaj commented Mar 24, 2026

r? clippy

@rustbot rustbot assigned Centri3 and unassigned Jarcho Mar 24, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 28, 2026

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

Copy link
Copy Markdown
Contributor

@ada4a ada4a left a comment

Choose a reason for hiding this comment

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

First pass over the code. A lot of the comments are stylistic, apologies for that -- it just makes the rest of the code easier to review^^

View changes since this review

Comment on lines +95 to +110
let TyKind::Path(
qpath @ QPath::Resolved(
_,
Path {
segments:
[
..,
PathSegment {
args: Some(hir_ty_generics),
..
},
],
..
},
),
) = hir_ty.kind
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the indentation is terrifying.. maybe use a let-chain instead?

Suggested change
let TyKind::Path(
qpath @ QPath::Resolved(
_,
Path {
segments:
[
..,
PathSegment {
args: Some(hir_ty_generics),
..
},
],
..
},
),
) = hir_ty.kind
let hir_ty_generics = if let TyKind::Path(ref qpath) = hir_ty.kind
&& let QPath::Resolved(_, path) = qpath
&& let Some(last_segment) = path.segments.last()
&& let Some(hir_ty_generics) = last_segment.args
{
hir_ty_generics
} else {
return;
};

redundant_tys_spans.push(redundant_ty.span());
}
}
let no_generics_hir_ty_snippet = snippet_opt(cx, hir_ty.span.until(hir_ty_generics.span_ext)).unwrap();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

unwrapping in clippy is generally not a good idea... it's preferable to use snippet, since it accepts a fallback string in case source code is not available (which can happen)

if i > 0 {
generics_string.push_str(", ");
}
generics_string.push_str(&snippet_opt(cx, arg.span()).unwrap());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

see previous comment

let (defaults, aliased_to_alias) = match_generics(cx, aliased_ty_args.as_slice(), alias_ty_params.as_slice());

let mut redundant_tys_spans = Vec::new();
for (i, generic_arg) in resolved_ty_args.iter().enumerate() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could replace with .into_iter() to avoid copying below

hir_ty.span,
format!(
"redudant usage of default argument `{}`",
snippet_opt(cx, redundant_ty_span).unwrap()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please use snippet

if expr.span.from_expansion() {
return;
}
let ty = cx.typeck_results().expr_ty(expr);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

does this one get used anywhere?

Comment on lines +640 to +676
check_typair(cx, (cx.typeck_results().node_type(ty.hir_id), *ty));
for ty_pair in path_generic_args(iter::once(segment))
.into_iter()
.map(|hir_ty| (cx.typeck_results().node_type(hir_ty.hir_id), hir_ty))
{
check_typair(cx, ty_pair);
}
},
QPath::Resolved(ty, path) => {
if let Some(ty) = ty {
check_typair(cx, (cx.typeck_results().node_type(ty.hir_id), *ty));
}
for ty_pair in path_generic_args(path.segments)
.into_iter()
.map(|hir_ty| (cx.typeck_results().node_type(hir_ty.hir_id), hir_ty))
{
check_typair(cx, ty_pair);
}
},
_ => {},
},
ExprKind::Closure(Closure {
fn_decl: FnDecl { inputs, output, .. },
..
}) => {
for input in *inputs {
check_typair(cx, (cx.typeck_results().node_type(input.hir_id), *input));
}
if let FnRetTy::Return(ty) = *output {
check_typair(cx, (cx.typeck_results().node_type(ty.hir_id), *ty));
}
},
ExprKind::Cast(_, &ty)
| ExprKind::Type(_, &ty)
| ExprKind::Let(&LetExpr { ty: Some(&ty), .. })
| ExprKind::UnsafeBinderCast(_, _, Some(&ty))
| ExprKind::OffsetOf(&ty, _) => check_typair(cx, (cx.typeck_results().expr_ty(expr), ty)),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you might want to do let typeck = cx.typeck_results(); to save yourself some characters across this match

Comment on lines +10 to +20
error: redudant usage of default argument `i32`
--> tests/ui/explicit_default_arguments.rs:28:28
|
LL | fn multi_all_defaults() -> Multi<i32, String> {
| ^^^^^^^^^^^^^^^^^^ help: try: `Multi`

error: redudant usage of default argument `String`
--> tests/ui/explicit_default_arguments.rs:28:28
|
LL | fn multi_all_defaults() -> Multi<i32, String> {
| ^^^^^^^^^^^^^^^^^^ help: try: `Multi`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. these two should ideally be one lint
  2. you don't need to repeat the name of the argument -- highlight its span instead
Suggested change
error: redudant usage of default argument `i32`
--> tests/ui/explicit_default_arguments.rs:28:28
|
LL | fn multi_all_defaults() -> Multi<i32, String> {
| ^^^^^^^^^^^^^^^^^^ help: try: `Multi`
error: redudant usage of default argument `String`
--> tests/ui/explicit_default_arguments.rs:28:28
|
LL | fn multi_all_defaults() -> Multi<i32, String> {
| ^^^^^^^^^^^^^^^^^^ help: try: `Multi`
error: redundant usage of default arguments
--> tests/ui/explicit_default_arguments.rs:28:28
|
LL | fn multi_all_defaults() -> Multi<i32, String> {
| ^^^ ^^^^^^
|
help: consider removing them
|
LL - fn multi_all_defaults() -> Multi<i32, String> {
LL + fn multi_all_defaults() -> Multi {
|

hir_ty.hir_id,
hir_ty.span,
format!(
"redudant usage of default argument `{}`",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
"redudant usage of default argument `{}`",
"redundant usage of default argument `{}`",

snippet_opt(cx, redundant_ty_span).unwrap()
),
|diag| {
diag.span_suggestion(hir_ty.span, "try", &sugg_text_snippet, Applicability::MachineApplicable);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider replacing hir_ty_generics.span_ext instead. That way, you will no longer need to include no_generics_hir_ty_snippet in sugg_text_snippet. As an added bonus, the suggestion should have a nicer diff (only the generics will be highlighted)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Apr 3, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 3, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@vogelbirb
Copy link
Copy Markdown
Author

Thanks for the thorough review. There's a lot to address, so I'll get to it as soon as I have the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lint idea: useless_default_generic_parameters

6 participants