Skip to content

Commit 40dddd3

Browse files
committed
use matches!() macro for simple if let conditions
1 parent 2c69266 commit 40dddd3

File tree

15 files changed

+33
-36
lines changed

15 files changed

+33
-36
lines changed

compiler/rustc_ast/src/ast.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1931,7 +1931,7 @@ pub enum TyKind {
19311931

19321932
impl TyKind {
19331933
pub fn is_implicit_self(&self) -> bool {
1934-
if let TyKind::ImplicitSelf = *self { true } else { false }
1934+
matches!(self, TyKind::ImplicitSelf)
19351935
}
19361936

19371937
pub fn is_unit(&self) -> bool {
@@ -2227,7 +2227,7 @@ pub enum Async {
22272227

22282228
impl Async {
22292229
pub fn is_async(self) -> bool {
2230-
if let Async::Yes { .. } = self { true } else { false }
2230+
matches!(self, Async::Yes { .. })
22312231
}
22322232

22332233
/// In this case this is an `async` return, the `NodeId` for the generated `impl Trait` item.
@@ -2508,7 +2508,7 @@ pub enum VisibilityKind {
25082508

25092509
impl VisibilityKind {
25102510
pub fn is_pub(&self) -> bool {
2511-
if let VisibilityKind::Public = *self { true } else { false }
2511+
matches!(self, VisibilityKind::Public)
25122512
}
25132513
}
25142514

compiler/rustc_ast_passes/src/ast_validation.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -868,10 +868,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
868868
.emit();
869869
}
870870

871-
if !bounds
872-
.iter()
873-
.any(|b| if let GenericBound::Trait(..) = *b { true } else { false })
874-
{
871+
if !bounds.iter().any(|b| matches!(b, GenericBound::Trait(..))) {
875872
self.err_handler().span_err(ty.span, "at least one trait must be specified");
876873
}
877874

compiler/rustc_attr/src/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ pub enum StabilityLevel {
160160

161161
impl StabilityLevel {
162162
pub fn is_unstable(&self) -> bool {
163-
if let StabilityLevel::Unstable { .. } = *self { true } else { false }
163+
matches!(self, StabilityLevel::Unstable { .. })
164164
}
165165
pub fn is_stable(&self) -> bool {
166-
if let StabilityLevel::Stable { .. } = *self { true } else { false }
166+
matches!(self, StabilityLevel::Stable { .. })
167167
}
168168
}
169169

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ impl<'a> TraitDef<'a> {
15291529
}
15301530
}
15311531

1532-
let is_tuple = if let ast::VariantData::Tuple(..) = struct_def { true } else { false };
1532+
let is_tuple = matches!(struct_def, ast::VariantData::Tuple(..));
15331533
match (just_spans.is_empty(), named_idents.is_empty()) {
15341534
(false, false) => cx.span_bug(
15351535
self.span,

compiler/rustc_errors/src/snippet.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,15 @@ pub struct Annotation {
118118
impl Annotation {
119119
/// Whether this annotation is a vertical line placeholder.
120120
pub fn is_line(&self) -> bool {
121-
if let AnnotationType::MultilineLine(_) = self.annotation_type { true } else { false }
121+
matches!(self.annotation_type, AnnotationType::MultilineLine(_))
122122
}
123123

124124
pub fn is_multiline(&self) -> bool {
125-
match self.annotation_type {
125+
matches!(self.annotation_type,
126126
AnnotationType::Multiline(_)
127127
| AnnotationType::MultilineStart(_)
128128
| AnnotationType::MultilineLine(_)
129-
| AnnotationType::MultilineEnd(_) => true,
130-
_ => false,
131-
}
129+
| AnnotationType::MultilineEnd(_))
132130
}
133131

134132
pub fn len(&self) -> usize {

compiler/rustc_lint/src/builtin.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1985,9 +1985,9 @@ impl ExplicitOutlivesRequirements {
19851985
.filter_map(|(i, bound)| {
19861986
if let hir::GenericBound::Outlives(lifetime) = bound {
19871987
let is_inferred = match tcx.named_region(lifetime.hir_id) {
1988-
Some(Region::Static) if infer_static => inferred_outlives
1989-
.iter()
1990-
.any(|r| if let ty::ReStatic = r { true } else { false }),
1988+
Some(Region::Static) if infer_static => {
1989+
inferred_outlives.iter().any(|r| matches!(r, ty::ReStatic))
1990+
}
19911991
Some(Region::EarlyBound(index, ..)) => inferred_outlives.iter().any(|r| {
19921992
if let ty::ReEarlyBound(ebr) = r { ebr.index == index } else { false }
19931993
}),
@@ -2079,9 +2079,10 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
20792079
let mut lint_spans = Vec::new();
20802080

20812081
for param in hir_generics.params {
2082-
let has_lifetime_bounds = param.bounds.iter().any(|bound| {
2083-
if let hir::GenericBound::Outlives(_) = bound { true } else { false }
2084-
});
2082+
let has_lifetime_bounds = param
2083+
.bounds
2084+
.iter()
2085+
.any(|bound| matches!(bound, hir::GenericBound::Outlives(_)));
20852086
if !has_lifetime_bounds {
20862087
continue;
20872088
}

compiler/rustc_middle/src/middle/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub enum LibSource {
6969

7070
impl LibSource {
7171
pub fn is_some(&self) -> bool {
72-
if let LibSource::Some(_) = *self { true } else { false }
72+
matches!(self, LibSource::Some(_))
7373
}
7474

7575
pub fn option(&self) -> Option<PathBuf> {

compiler/rustc_mir/src/borrow_check/diagnostics/outlives_suggestion.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ impl OutlivesSuggestionBuilder {
115115
// should just replace 'a with 'static.
116116
// 3) Suggest unifying 'a with 'b if we have both 'a: 'b and 'b: 'a
117117

118-
if outlived.iter().any(|(_, outlived_name)| {
119-
if let RegionNameSource::Static = outlived_name.source { true } else { false }
120-
}) {
118+
if outlived
119+
.iter()
120+
.any(|(_, outlived_name)| matches!(outlived_name.source, RegionNameSource::Static))
121+
{
121122
suggested.push(SuggestedConstraint::Static(fr_name));
122123
} else {
123124
// We want to isolate out all lifetimes that should be unified and print out

compiler/rustc_mir/src/transform/promote_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub enum TempState {
9292
impl TempState {
9393
pub fn is_promotable(&self) -> bool {
9494
debug!("is_promotable: self={:?}", self);
95-
if let TempState::Defined { .. } = *self { true } else { false }
95+
matches!(self, TempState::Defined { .. } )
9696
}
9797
}
9898

compiler/rustc_mir/src/transform/simplify.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
281281

282282
fn strip_nops(&mut self) {
283283
for blk in self.basic_blocks.iter_mut() {
284-
blk.statements
285-
.retain(|stmt| if let StatementKind::Nop = stmt.kind { false } else { true })
284+
blk.statements.retain(|stmt| !matches!(stmt.kind, StatementKind::Nop))
286285
}
287286
}
288287
}

compiler/rustc_mir_build/src/build/block.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9696
);
9797
}
9898
StmtKind::Let { remainder_scope, init_scope, pattern, initializer, lint_level } => {
99-
let ignores_expr_result =
100-
if let PatKind::Wild = *pattern.kind { true } else { false };
99+
let ignores_expr_result = matches!(*pattern.kind, PatKind::Wild);
101100
this.block_context.push(BlockFrame::Statement { ignores_expr_result });
102101

103102
// Enter the remainder scope, i.e., the bindings' destruction scope.

compiler/rustc_mir_build/src/build/matches/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17931793
.flat_map(|(bindings, _)| bindings)
17941794
.chain(&candidate.bindings)
17951795
.filter(|binding| {
1796-
if let BindingMode::ByValue = binding.binding_mode { true } else { false }
1796+
matches!(binding.binding_mode, BindingMode::ByValue )
17971797
});
17981798
// Read all of the by reference bindings to ensure that the
17991799
// place they refer to can't be modified by the guard.

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
395395
// so prefixes are prepended with crate root segment if necessary.
396396
// The root is prepended lazily, when the first non-empty prefix or terminating glob
397397
// appears, so imports in braced groups can have roots prepended independently.
398-
let is_glob = if let ast::UseTreeKind::Glob = use_tree.kind { true } else { false };
398+
let is_glob = matches!(use_tree.kind, ast::UseTreeKind::Glob);
399399
let crate_root = match prefix_iter.peek() {
400400
Some(seg) if !seg.ident.is_path_segment_keyword() && seg.ident.span.rust_2015() => {
401401
Some(seg.ident.span.ctxt())

compiler/rustc_resolve/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10341034
let mut add_bindings_for_ns = |ns| {
10351035
let parent_rib = self.ribs[ns]
10361036
.iter()
1037-
.rfind(|r| if let ItemRibKind(_) = r.kind { true } else { false })
1037+
.rfind(|r| matches!(r.kind, ItemRibKind(_)))
10381038
.expect("associated item outside of an item");
10391039
seen_bindings
10401040
.extend(parent_rib.bindings.iter().map(|(ident, _)| (*ident, ident.span)));

compiler/rustc_typeck/src/check/expr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
439439
// This is maybe too permissive, since it allows
440440
// `let u = &raw const Box::new((1,)).0`, which creates an
441441
// immediately dangling raw pointer.
442-
self.typeck_results.borrow().adjustments().get(base.hir_id).map_or(false, |x| {
443-
x.iter().any(|adj| if let Adjust::Deref(_) = adj.kind { true } else { false })
444-
})
442+
self.typeck_results
443+
.borrow()
444+
.adjustments()
445+
.get(base.hir_id)
446+
.map_or(false, |x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_))))
445447
});
446448
if !is_named {
447449
self.tcx.sess.emit_err(AddressOfTemporaryTaken { span: oprnd.span })

0 commit comments

Comments
 (0)