Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 39219a6

Browse files
authored
Rollup merge of rust-lang#114819 - estebank:issue-78124, r=compiler-errors
Point at return type when it influences non-first `match` arm When encountering code like ```rust fn foo() -> i32 { match 0 { 1 => return 0, 2 => "", _ => 1, } } ``` Point at the return type and not at the prior arm, as that arm has type `!` which isn't influencing the arm corresponding to arm `2`. Fix rust-lang#78124.
2 parents 9e33b69 + d0a26f1 commit 39219a6

16 files changed

+17
-35
lines changed

clippy_lints/src/dereference.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ fn in_postfix_position<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> boo
802802
match parent.kind {
803803
ExprKind::Call(child, _) | ExprKind::MethodCall(_, child, _, _) | ExprKind::Index(child, _, _)
804804
if child.hir_id == e.hir_id => true,
805-
ExprKind::Field(_, _) | ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar) => true,
805+
ExprKind::Match(.., MatchSource::TryDesugar(_) | MatchSource::AwaitDesugar)
806+
| ExprKind::Field(_, _) => true,
806807
_ => false,
807808
}
808809
} else {

clippy_lints/src/matches/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
10381038
wild_in_or_pats::check(cx, arms);
10391039
}
10401040

1041-
if source == MatchSource::TryDesugar {
1041+
if let MatchSource::TryDesugar(_) = source {
10421042
try_err::check(cx, expr, ex);
10431043
}
10441044

clippy_lints/src/matches/try_err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
8080

8181
/// Finds function return type by examining return expressions in match arms.
8282
fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
83-
if let ExprKind::Match(_, arms, MatchSource::TryDesugar) = expr {
83+
if let ExprKind::Match(_, arms, MatchSource::TryDesugar(_)) = expr {
8484
for arm in *arms {
8585
if let ExprKind::Ret(Some(ret)) = arm.body.kind {
8686
return Some(cx.typeck_results().expr_ty(ret));

clippy_lints/src/methods/clone_on_copy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(super) fn check(
6464
ExprKind::Path(QPath::LangItem(rustc_hir::LangItem::TryTraitBranch, _, _))
6565
),
6666
ExprKind::MethodCall(_, self_arg, ..) if expr.hir_id == self_arg.hir_id => true,
67-
ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
67+
ExprKind::Match(_, _, MatchSource::TryDesugar(_) | MatchSource::AwaitDesugar)
6868
| ExprKind::Field(..)
6969
| ExprKind::Index(..) => true,
7070
_ => false,

clippy_lints/src/methods/str_splitn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn indirect_usage<'tcx>(
236236
!matches!(
237237
node,
238238
Node::Expr(Expr {
239-
kind: ExprKind::Match(.., MatchSource::TryDesugar),
239+
kind: ExprKind::Match(.., MatchSource::TryDesugar(_)),
240240
..
241241
})
242242
)

clippy_lints/src/needless_question_mark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
122122
} else {
123123
return;
124124
};
125-
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar) = &arg.kind;
125+
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar(_)) = &arg.kind;
126126
if let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind;
127127
if let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)) = &called.kind;
128128
if expr.span.ctxt() == inner_expr.span.ctxt();

clippy_lints/src/question_mark_used.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ declare_lint_pass!(QuestionMarkUsed => [QUESTION_MARK_USED]);
3434

3535
impl<'tcx> LateLintPass<'tcx> for QuestionMarkUsed {
3636
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
37-
if let ExprKind::Match(_, _, MatchSource::TryDesugar) = expr.kind {
37+
if let ExprKind::Match(_, _, MatchSource::TryDesugar(_)) = expr.kind {
3838
if !span_is_local(expr.span) {
3939
return;
4040
}

clippy_lints/src/redundant_closure_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl ReturnVisitor {
5252

5353
impl<'tcx> Visitor<'tcx> for ReturnVisitor {
5454
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
55-
if let hir::ExprKind::Ret(_) | hir::ExprKind::Match(.., hir::MatchSource::TryDesugar) = ex.kind {
55+
if let hir::ExprKind::Ret(_) | hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_)) = ex.kind {
5656
self.found_return = true;
5757
} else {
5858
hir_visit::walk_expr(self, ex);

clippy_lints/src/returns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<'tcx> LateLintPass<'tcx> for Return {
164164
if !in_external_macro(cx.sess(), stmt.span)
165165
&& let StmtKind::Semi(expr) = stmt.kind
166166
&& let ExprKind::Ret(Some(ret)) = expr.kind
167-
&& let ExprKind::Match(.., MatchSource::TryDesugar) = ret.kind
167+
&& let ExprKind::Match(.., MatchSource::TryDesugar(_)) = ret.kind
168168
// Ensure this is not the final stmt, otherwise removing it would cause a compile error
169169
&& let OwnerNode::Item(item) = cx.tcx.hir().owner(cx.tcx.hir().get_parent_item(expr.hir_id))
170170
&& let ItemKind::Fn(_, _, body) = item.kind

clippy_lints/src/unit_types/unit_arg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
4242
if cx.typeck_results().expr_ty(arg).is_unit() && !utils::is_unit_literal(arg) {
4343
!matches!(
4444
&arg.kind,
45-
ExprKind::Match(.., MatchSource::TryDesugar) | ExprKind::Path(..)
45+
ExprKind::Match(.., MatchSource::TryDesugar(_)) | ExprKind::Path(..)
4646
)
4747
} else {
4848
false

0 commit comments

Comments
 (0)