10000 Apply #[must_use] lint to components of tuples by varkor · Pull Request #61100 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Apply #[must_use] lint to components of tuples #61100

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

Merged
merged 7 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use precise span for must_use tuple components
  • Loading branch information
varkor committed Jun 3, 2019
commit e121d9671afe4eae1f418db14a6fdae07652c51c
19 changes: 15 additions & 4 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
{
true
} else {
check_must_use_ty(cx, ty, s.span)
check_must_use_ty(cx, ty, &expr, s.span)
};

let mut fn_warned = false;
Expand Down Expand Up @@ -138,6 +138,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
fn check_must_use_ty(
cx: &LateContext<'_, '_>,
ty: Ty<'_>,
expr: &hir::Expr,
span: Span,
) -> bool {
match ty.sty {
Expand Down Expand Up @@ -170,9 +171,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
has_emitted
}
ty::Tuple(ref tys) => {
tys.iter().map(|k| k.expect_ty()).any(|ty| {
check_must_use_ty(cx, ty, span)
})
let mut has_emitted = false;
let spans = if let hir::ExprKind::Tup(comps) = &expr.node {
debug_assert_eq!(comps.len(), tys.len());
comps.iter().map(|e| e.span).collect()
} else {
vec![]
};
for (i, ty) in tys.iter().map(|k| k.expect_ty()).enumerate() {
if check_must_use_ty(cx, ty, expr, *spans.get(i).unwrap_or(&span)) {
has_emitted = true;
}
}
has_emitted
}
_ => false,
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/lint/must_use-tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

fn main() {
(Ok::<(), ()>(()),); //~ ERROR unused `std::result::Result` that must be used

(Ok::<(), ()>(()), 0, Ok::<(), ()>(()), 5);
//~^ ERROR unused `std::result::Result` that must be used
//~^^ ERROR unused `std::result::Result` that must be used
}
22 changes: 19 additions & 3 deletions src/test/ui/lint/must_use-tuple.stderr
< 844D td class="blob-code blob-code-addition js-file-line"> --> $DIR/must_use-tuple.rs:4:6
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: unused `std::result::Result` that must be used
--> $DIR/must_use-tuple.rs:4:5
|
LL | (Ok::<(), ()>(()),);
| ^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^
|
note: lint level defined here
--> $DIR/must_use-tuple.rs:1:9
Expand All @@ -11,5 +11,21 @@ LL | #![deny(unused_must_use)]
| ^^^^^^^^^^^^^^^
= note: this `Result` may be an `Err` variant, which should be handled

error: aborting due to previous error
error: unused `std::result::Result` that must be used
--> $DIR/must_use-tuple.rs:6:6
|
LL | (Ok::<(), ()>(()), 0, Ok::<(), ()>(()), 5);
| ^^^^^^^^^^^^^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled

error: unused `std::result::Result` that must be used
--> $DIR/must_use-tuple.rs:6:27
|
LL | (Ok::<(), ()>(()), 0, Ok::<(), ()>(()), 5);
| ^^^^^^^^^^^^^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled

error: aborting due to 3 previous errors

0