Skip to content

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

clippy_lints/src/useless_conversion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
113113
}
114114

115115
match e.kind {
116-
ExprKind::Match(_, arms, MatchSource::TryDesugar) => {
116+
ExprKind::Match(_, arms, MatchSource::TryDesugar(_)) => {
117117
let (ExprKind::Ret(Some(e)) | ExprKind::Break(_, Some(e))) = arms[0].body.kind else {
118118
return;
119119
};

clippy_utils/src/check_proc_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn expr_search_pat(tcx: TyCtxt<'_>, e: &Expr<'_>) -> (Pat, Pat) {
149149
(Pat::Str("for"), Pat::Str("}"))
150150
},
151151
ExprKind::Match(_, _, MatchSource::Normal) => (Pat::Str("match"), Pat::Str("}")),
152-
ExprKind::Match(e, _, MatchSource::TryDesugar) => (expr_search_pat(tcx, e).0, Pat::Str("?")),
152+
ExprKind::Match(e, _, MatchSource::TryDesugar(_)) => (expr_search_pat(tcx, e).0, Pat::Str("?")),
153153
ExprKind::Match(e, _, MatchSource::AwaitDesugar) | ExprKind::Yield(e, YieldSource::Await { .. }) => {
154154
(expr_search_pat(tcx, e).0, Pat::Str("await"))
155155
},

clippy_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ pub fn is_try<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'tcx>) -> Option<&'tc
17651765

17661766
if let ExprKind::Match(_, arms, ref source) = expr.kind {
17671767
// desugared from a `?` operator
1768-
if *source == MatchSource::TryDesugar {
1768+
if let MatchSource::TryDesugar(_) = *source {
17691769
return Some(expr);
17701770
}
17711771

clippy_utils/src/visitors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub fn for_each_expr_with_closures<'tcx, B, C: Continue>(
161161
/// returns `true` if expr contains match expr desugared from try
162162
fn contains_try(expr: &hir::Expr<'_>) -> bool {
163163
for_each_expr(expr, |e| {
164-
if matches!(e.kind, hir::ExprKind::Match(_, _, hir::MatchSource::TryDesugar)) {
164+
if matches!(e.kind, hir::ExprKind::Match(_, _, hir::MatchSource::TryDesugar(_))) {
165165
ControlFlow::Break(())
166166
} else {
167167
ControlFlow::Continue(())

tests/ui/if_same_then_else2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn if_same_then_else2() -> Result<&'static str, ()> {
9898
};
9999

100100
if true {
101-
//~^ ERROR: this `if` has identical blocks
101+
// FIXME: should emit "this `if` has identical blocks"
102102
Ok("foo")?;
103103
} else {
104104
Ok("foo")?;

tests/ui/if_same_then_else2.stderr

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,6 @@ LL | | f32::NAN
8282
LL | | };
8383
| |_____^
8484

85-
error: this `if` has identical blocks
86-
--> $DIR/if_same_then_else2.rs:100:13
87-
|
88-
LL | if true {
89-
| _____________^
90-
LL | |
91-
LL | | Ok("foo")?;
92-
LL | | } else {
93-
| |_____^
94-
|
95-
note: same as this
96-
--> $DIR/if_same_then_else2.rs:103:12
97-
|
98-
LL | } else {
99-
| ____________^
100-
LL | | Ok("foo")?;
101-
LL | | }
102-
| |_____^
103-
10485
error: this `if` has identical blocks
10586
--> $DIR/if_same_then_else2.rs:124:20
10687
|
@@ -122,5 +103,5 @@ LL | | return Ok(&foo[0..]);
122103
LL | | }
123104
| |_____^
124105

125-
error: aborting due to 6 previous errors
106+
error: aborting due to 5 previous errors
126107

0 commit comments

Comments
 (0)