Skip to content
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

Fix clippy lint for identical if/else contraining ? expressions #115122

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_utils/src/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc_hir::{
GenericArgs, Guard, HirId, HirIdMap, InlineAsmOperand, Let, Lifetime, LifetimeName, Pat, PatField, PatKind, Path,
PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding,
};
use rustc_hir::MatchSource::TryDesugar;
use rustc_lexer::{tokenize, TokenKind};
use rustc_lint::LateContext;
use rustc_middle::ty::TypeckResults;
Expand Down Expand Up @@ -311,7 +312,7 @@ impl HirEqInterExpr<'_, '_, '_> {
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name)
},
(&ExprKind::Match(le, la, ref ls), &ExprKind::Match(re, ra, ref rs)) => {
ls == rs
(ls == rs || (matches!((ls, rs), (TryDesugar(_), TryDesugar(_)))))
&& self.eq_expr(le, re)
&& over(la, ra, |l, r| {
self.eq_pat(l.pat, r.pat)
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/if_same_then_else2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn if_same_then_else2() -> Result<&'static str, ()> {
};

if true {
// FIXME: should emit "this `if` has identical blocks"
//~^ ERROR: this `if` has identical blocks
Ok("foo")?;
} else {
Ok("foo")?;
Expand Down
21 changes: 20 additions & 1 deletion src/tools/clippy/tests/ui/if_same_then_else2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ LL | | f32::NAN
LL | | };
| |_____^

error: this `if` has identical blocks
--> $DIR/if_same_then_else2.rs:100:13
|
LL | if true {
| _____________^
LL | |
LL | | Ok("foo")?;
LL | | } else {
| |_____^
|
note: same as this
--> $DIR/if_same_then_else2.rs:103:12
|
LL | } else {
| ____________^
LL | | Ok("foo")?;
LL | | }
| |_____^

error: this `if` has identical blocks
--> $DIR/if_same_then_else2.rs:124:20
|
Expand All @@ -103,5 +122,5 @@ LL | | return Ok(&foo[0..]);
LL | | }
| |_____^

error: aborting due to 5 previous errors
error: aborting due to 6 previous errors

Loading