Fix unused lint error in macro
This commit is contained in:
parent
9f6cd6defb
commit
87f75df0b8
3 changed files with 50 additions and 5 deletions
|
|
@ -539,10 +539,19 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
|
|||
);
|
||||
}
|
||||
MustUsePath::Def(span, def_id, reason) => {
|
||||
let span = span.find_ancestor_not_from_macro().unwrap_or(*span);
|
||||
let ancenstor_span = span.find_ancestor_not_from_macro().unwrap_or(*span);
|
||||
let is_redundant_let_ignore = cx
|
||||
.sess()
|
||||
.source_map()
|
||||
.span_to_prev_source(ancenstor_span)
|
||||
.ok()
|
||||
.map(|prev| prev.trim_end().ends_with("let _ ="))
|
||||
.unwrap_or(false);
|
||||
let suggestion_span =
|
||||
if is_redundant_let_ignore { *span } else { ancenstor_span };
|
||||
cx.emit_span_lint(
|
||||
UNUSED_MUST_USE,
|
||||
span,
|
||||
ancenstor_span,
|
||||
UnusedDef {
|
||||
pre: descr_pre,
|
||||
post: descr_post,
|
||||
|
|
@ -551,11 +560,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
|
|||
note: *reason,
|
||||
suggestion: (!is_inner).then_some(if expr_is_from_block {
|
||||
UnusedDefSuggestion::BlockTailExpr {
|
||||
before_span: span.shrink_to_lo(),
|
||||
after_span: span.shrink_to_hi(),
|
||||
before_span: suggestion_span.shrink_to_lo(),
|
||||
after_span: suggestion_span.shrink_to_hi(),
|
||||
}
|
||||
} else {
|
||||
UnusedDefSuggestion::NormalExpr { span: span.shrink_to_lo() }
|
||||
UnusedDefSuggestion::NormalExpr {
|
||||
span: suggestion_span.shrink_to_lo(),
|
||||
}
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
|
|
|||
15
tests/ui/lint/unused/lint-unsed-in-macro-issue-151269.rs
Normal file
15
tests/ui/lint/unused/lint-unsed-in-macro-issue-151269.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#![deny(unused_must_use)]
|
||||
|
||||
fn error() -> Result<(), ()> {
|
||||
Err(())
|
||||
}
|
||||
|
||||
macro_rules! foo {
|
||||
() => {{
|
||||
error();
|
||||
}};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = foo!(); //~ ERROR unused `Result` that must be used
|
||||
}
|
||||
19
tests/ui/lint/unused/lint-unsed-in-macro-issue-151269.stderr
Normal file
19
tests/ui/lint/unused/lint-unsed-in-macro-issue-151269.stderr
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
error: unused `Result` that must be used
|
||||
--> $DIR/lint-unsed-in-macro-issue-151269.rs:14:13
|
||||
|
|
||||
LL | let _ = foo!();
|
||||
| ^^^^^^
|
||||
|
|
||||
= note: this `Result` may be an `Err` variant, which should be handled
|
||||
note: the lint level is defined here
|
||||
--> $DIR/lint-unsed-in-macro-issue-151269.rs:1:9
|
||||
|
|
||||
LL | #![deny(unused_must_use)]
|
||||
| ^^^^^^^^^^^^^^^
|
||||
help: use `let _ = ...` to ignore the resulting value
|
||||
|
|
||||
LL | let _ = error();
|
||||
| +++++++
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue