Simplify some conditions

This commit is contained in:
Maybe Waffle 2023-06-27 07:40:47 +00:00
parent 97bf23d26b
commit ef05533c39
11 changed files with 35 additions and 45 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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) {

View File

@ -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)

View File

@ -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;

View File

@ -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`",

View File

@ -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);

View File

@ -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)
})
})
}

View File

@ -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

View File

@ -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,

View File

@ -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<Symbol, Vec<Span>>) {
let mut inner = self.spans.borrow_mut();