From ef05533c39a8c2009540366731b7459a770f8a8b Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 27 Jun 2023 07:40:47 +0000 Subject: [PATCH] Simplify some conditions --- compiler/rustc_ast_passes/src/ast_validation.rs | 7 +++---- compiler/rustc_expand/src/config.rs | 2 +- .../src/coherence/inherent_impls_overlap.rs | 10 +++++----- compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs | 4 ++-- compiler/rustc_middle/src/lint.rs | 7 ++++--- .../rustc_mir_transform/src/coverage/graph.rs | 2 +- .../rustc_mir_transform/src/coverage/spans.rs | 7 ++++--- compiler/rustc_parse/src/parser/attr.rs | 17 +++++++---------- compiler/rustc_parse/src/parser/attr_wrapper.rs | 10 +++++----- compiler/rustc_resolve/src/late/diagnostics.rs | 7 +++---- compiler/rustc_session/src/parse.rs | 7 ------- 11 files changed, 35 insertions(+), 45 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index a0979bbda5452..096cea945b029 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -623,13 +623,12 @@ impl<'a> AstValidator<'a> { fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId) { // FIXME(davidtwco): This is a hack to detect macros which produce spans of the // call site which do not have a macro backtrace. See #61963. - let is_macro_callsite = self + if self .session .source_map() .span_to_snippet(span) - .map(|snippet| snippet.starts_with("#[")) - .unwrap_or(true); - if !is_macro_callsite { + .is_ok_and(|snippet| !snippet.starts_with("#[")) + { self.lint_buffer.buffer_lint_with_diagnostic( MISSING_ABI, id, diff --git a/compiler/rustc_expand/src/config.rs b/compiler/rustc_expand/src/config.rs index bcfa5313bde3a..3e43eae006fdd 100644 --- a/compiler/rustc_expand/src/config.rs +++ b/compiler/rustc_expand/src/config.rs @@ -445,7 +445,7 @@ impl<'a> StripUnconfigured<'a> { /// If attributes are not allowed on expressions, emit an error for `attr` #[instrument(level = "trace", skip(self))] pub(crate) fn maybe_emit_expr_attr_err(&self, attr: &Attribute) { - if !self.features.map_or(true, |features| features.stmt_expr_attributes) { + if self.features.is_some_and(|features| !features.stmt_expr_attributes) { let mut err = feature_err( &self.sess.parse_sess, sym::stmt_expr_attributes, diff --git a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs index bd6252344b2c2..3bd2931265c73 100644 --- a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs +++ b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs @@ -140,7 +140,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> { impl1_def_id: DefId, impl2_def_id: DefId, ) { - traits::overlapping_impls( + let maybe_overlap = traits::overlapping_impls( self.tcx, impl1_def_id, impl2_def_id, @@ -148,11 +148,11 @@ impl<'tcx> InherentOverlapChecker<'tcx> { // inherent impls without warning. SkipLeakCheck::Yes, overlap_mode, - ) - .map_or(true, |overlap| { + ); + + if let Some(overlap) = maybe_overlap { self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap); - false - }); + } } fn check_item(&mut self, id: hir::ItemId) { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index f294ff0051df5..5bd069fc275d2 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -955,9 +955,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // - f(0, 1,) // + f() if only_extras_so_far - && errors + && !errors .peek() - .map_or(true, |next_error| !matches!(next_error, Error::Extra(_))) + .is_some_and(|next_error| matches!(next_error, Error::Extra(_))) { let next = provided_arg_tys .get(arg_idx + 1) diff --git a/compiler/rustc_middle/src/lint.rs b/compiler/rustc_middle/src/lint.rs index caf3fc26039a0..81c1ae4f6300e 100644 --- a/compiler/rustc_middle/src/lint.rs +++ b/compiler/rustc_middle/src/lint.rs @@ -388,10 +388,11 @@ pub fn struct_lint_level( // it'll become a hard error, so we have to emit *something*. Also, // if this lint occurs in the expansion of a macro from an external crate, // allow individual lints to opt-out from being reported. - let not_future_incompatible = - future_incompatible.map(|f| f.reason.edition().is_some()).unwrap_or(true); - if not_future_incompatible && !lint.report_in_external_macro { + let incompatible = future_incompatible.is_some_and(|f| f.reason.edition().is_none()); + + if !incompatible && !lint.report_in_external_macro { err.cancel(); + // Don't continue further, since we don't want to have // `diag_span_note_once` called for a diagnostic that isn't emitted. return; diff --git a/compiler/rustc_mir_transform/src/coverage/graph.rs b/compiler/rustc_mir_transform/src/coverage/graph.rs index ea1223fbca642..d2a854b26756d 100644 --- a/compiler/rustc_mir_transform/src/coverage/graph.rs +++ b/compiler/rustc_mir_transform/src/coverage/graph.rs @@ -387,7 +387,7 @@ impl BasicCoverageBlockData { // If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also // have an expression (to be injected into an existing `BasicBlock` represented by this // `BasicCoverageBlock`). - if !self.counter_kind.as_ref().map_or(true, |c| c.is_expression()) { + if self.counter_kind.as_ref().is_some_and(|c| !c.is_expression()) { return Error::from_string(format!( "attempt to add an incoming edge counter from {:?} when the target BCB already \ has a `Counter`", diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index d27200419e2cb..35cf9ea5f91cd 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -480,9 +480,10 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> { fn check_invoked_macro_name_span(&mut self) { if let Some(visible_macro) = self.curr().visible_macro(self.body_span) { - if self.prev_expn_span.map_or(true, |prev_expn_span| { - self.curr().expn_span.ctxt() != prev_expn_span.ctxt() - }) { + if !self + .prev_expn_span + .is_some_and(|prev_expn_span| self.curr().expn_span.ctxt() == prev_expn_span.ctxt()) + { let merged_prefix_len = self.curr_original_span.lo() - self.curr().span.lo(); let after_macro_bang = merged_prefix_len + BytePos(visible_macro.as_str().len() as u32 + 1); diff --git a/compiler/rustc_parse/src/parser/attr.rs b/compiler/rustc_parse/src/parser/attr.rs index e1db19557cfe2..ee0abba1c1753 100644 --- a/compiler/rustc_parse/src/parser/attr.rs +++ b/compiler/rustc_parse/src/parser/attr.rs @@ -422,15 +422,12 @@ impl<'a> Parser<'a> { } } -pub fn maybe_needs_tokens(attrs: &[ast::Attribute]) -> bool { - // One of the attributes may either itself be a macro, - // or expand to macro attributes (`cfg_attr`). - attrs.iter().any(|attr| { - if attr.is_doc_comment() { - return false; - } - attr.ident().map_or(true, |ident| { - ident.name == sym::cfg_attr || !rustc_feature::is_builtin_attr_name(ident.name) - }) +/// The attributes are complete if all attributes are either a doc comment or a builtin attribute other than `cfg_attr` +pub fn is_complete(attrs: &[ast::Attribute]) -> bool { + attrs.iter().all(|attr| { + attr.is_doc_comment() + || attr.ident().is_some_and(|ident| { + ident.name != sym::cfg_attr && rustc_feature::is_builtin_attr_name(ident.name) + }) }) } diff --git a/compiler/rustc_parse/src/parser/attr_wrapper.rs b/compiler/rustc_parse/src/parser/attr_wrapper.rs index 1e6ac54964f99..b579da098d8a1 100644 --- a/compiler/rustc_parse/src/parser/attr_wrapper.rs +++ b/compiler/rustc_parse/src/parser/attr_wrapper.rs @@ -61,8 +61,8 @@ impl AttrWrapper { self.attrs.is_empty() } - pub fn maybe_needs_tokens(&self) -> bool { - crate::parser::attr::maybe_needs_tokens(&self.attrs) + pub fn is_complete(&self) -> bool { + crate::parser::attr::is_complete(&self.attrs) } } @@ -201,7 +201,7 @@ impl<'a> Parser<'a> { // by definition if matches!(force_collect, ForceCollect::No) // None of our outer attributes can require tokens (e.g. a proc-macro) - && !attrs.maybe_needs_tokens() + && attrs.is_complete() // If our target supports custom inner attributes, then we cannot bail // out early, since we may need to capture tokens for a custom inner attribute // invocation. @@ -244,9 +244,9 @@ impl<'a> Parser<'a> { // Now that we've parsed an AST node, we have more information available. if matches!(force_collect, ForceCollect::No) // We now have inner attributes available, so this check is more precise - // than `attrs.maybe_needs_tokens()` at the start of the function. + // than `attrs.is_complete()` at the start of the function. // As a result, we don't need to check `R::SUPPORTS_CUSTOM_INNER_ATTRS` - && !crate::parser::attr::maybe_needs_tokens(ret.attrs()) + && crate::parser::attr::is_complete(ret.attrs()) // Subtle: We call `has_cfg_or_cfg_attr` with the attrs from `ret`. // This ensures that we consider inner attributes (e.g. `#![cfg]`), // which require us to have tokens available diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 7284b33f09d8e..0d62825b1c545 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2182,10 +2182,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { None => { debug!(?param.ident, ?param.ident.span); let deletion_span = deletion_span(); - // the give lifetime originates from expanded code so we won't be able to remove it #104432 - let lifetime_only_in_expanded_code = - deletion_span.map(|sp| sp.in_derive_expansion()).unwrap_or(true); - if !lifetime_only_in_expanded_code { + + // if the lifetime originates from expanded code, we won't be able to remove it #104432 + if deletion_span.is_some_and(|sp| !sp.in_derive_expansion()) { self.r.lint_buffer.buffer_lint_with_diagnostic( lint::builtin::UNUSED_LIFETIMES, param.id, diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index a433e2371c9d4..194f7201ff357 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -51,13 +51,6 @@ impl GatedSpans { debug_assert_eq!(span, removed_span); } - /// Is the provided `feature` gate ungated currently? - /// - /// Using this is discouraged unless you have a really good reason to. - pub fn is_ungated(&self, feature: Symbol) -> bool { - self.spans.borrow().get(&feature).map_or(true, |spans| spans.is_empty()) - } - /// Prepend the given set of `spans` onto the set in `self`. pub fn merge(&self, mut spans: FxHashMap>) { let mut inner = self.spans.borrow_mut();