-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
error-msg: expand suggestion for unused_def
lint
#109158
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,7 +123,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { | |
let must_use_result = is_ty_must_use(cx, ty, &expr, expr.span); | ||
let type_lint_emitted_or_suppressed = match must_use_result { | ||
Some(path) => { | ||
emit_must_use_untranslated(cx, &path, "", "", 1); | ||
emit_must_use_untranslated(cx, &path, "", "", 1, false); | ||
true | ||
} | ||
None => false, | ||
|
@@ -358,6 +358,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { | |
descr_pre_path, | ||
descr_post_path, | ||
1, | ||
false, | ||
) | ||
}) | ||
.is_some() | ||
|
@@ -370,27 +371,30 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { | |
descr_pre: &str, | ||
descr_post: &str, | ||
plural_len: usize, | ||
is_inner: bool, | ||
) { | ||
let plural_suffix = pluralize!(plural_len); | ||
|
||
match path { | ||
MustUsePath::Suppressed => {} | ||
MustUsePath::Boxed(path) => { | ||
let descr_pre = &format!("{}boxed ", descr_pre); | ||
emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len); | ||
emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len, true); | ||
} | ||
MustUsePath::Opaque(path) => { | ||
let descr_pre = &format!("{}implementer{} of ", descr_pre, plural_suffix); | ||
emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len); | ||
emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len, true); | ||
} | ||
MustUsePath::TraitObject(path) => { | ||
let descr_post = &format!(" trait object{}{}", plural_suffix, descr_post); | ||
emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len); | ||
emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len, true); | ||
} | ||
MustUsePath::TupleElement(elems) => { | ||
for (index, path) in elems { | ||
let descr_post = &format!(" in tuple element {}", index); | ||
emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len); | ||
emit_must_use_untranslated( | ||
cx, path, descr_pre, descr_post, plural_len, true, | ||
); | ||
} | ||
} | ||
MustUsePath::Array(path, len) => { | ||
|
@@ -401,6 +405,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { | |
descr_pre, | ||
descr_post, | ||
plural_len.saturating_add(usize::try_from(*len).unwrap_or(usize::MAX)), | ||
true, | ||
); | ||
} | ||
MustUsePath::Closure(span) => { | ||
|
@@ -418,19 +423,6 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { | |
); | ||
} | ||
MustUsePath::Def(span, def_id, reason) => { | ||
let suggestion = if matches!( | ||
cx.tcx.get_diagnostic_name(*def_id), | ||
Some(sym::add) | ||
| Some(sym::sub) | ||
| Some(sym::mul) | ||
| Some(sym::div) | ||
| Some(sym::rem) | ||
| Some(sym::neg), | ||
) { | ||
Some(UnusedDefSuggestion::Default { span: span.shrink_to_lo() }) | ||
} else { | ||
None | ||
}; | ||
cx.emit_spanned_lint( | ||
UNUSED_MUST_USE, | ||
*span, | ||
|
@@ -440,7 +432,8 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { | |
cx, | ||
def_id: *def_id, | ||
note: *reason, | ||
suggestion, | ||
suggestion: (!is_inner) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why only for the outermost one and not nested ones as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah right, the spans |
||
.then_some(UnusedDefSuggestion { span: span.shrink_to_lo() }), | ||
}, | ||
); | ||
} | ||
|
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.
Maybe we could suggest using
Instead? It silences the warning and looks a bit nicer & shorter IMO.
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.
I disagree,
let _
is more widely used and understood and also nicer imo.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.
Why is it more idiomatic? I think the only reason
let _
is used more, is that it was available since forever, while_
was added after that.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.
Also, keep in mind that it is about more than just ignoring a value; you might want to bind it to a variable. Removing
let
preemptively makes using the value as opposed to ignoring it harder.Also I'd like to keep the
let
anyway because it's much more common and IMHO conveys intent better.