Format all the let chains in compiler
This commit is contained in:
parent
2763ca50da
commit
b2d2184ede
206 changed files with 3120 additions and 2228 deletions
|
@ -120,9 +120,12 @@ impl<'tcx> LateLintPass<'tcx> for AsyncFnInTrait {
|
|||
def.owner_id.def_id,
|
||||
" + Send",
|
||||
);
|
||||
cx.tcx.emit_spanned_lint(ASYNC_FN_IN_TRAIT, item.hir_id(), async_span, AsyncFnInTraitDiag {
|
||||
sugg
|
||||
});
|
||||
cx.tcx.emit_spanned_lint(
|
||||
ASYNC_FN_IN_TRAIT,
|
||||
item.hir_id(),
|
||||
async_span,
|
||||
AsyncFnInTraitDiag { sugg },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -121,16 +121,14 @@ impl EarlyLintPass for WhileTrue {
|
|||
{
|
||||
let condition_span = e.span.with_hi(cond.span.hi());
|
||||
let replace = format!(
|
||||
"{}loop",
|
||||
label.map_or_else(String::new, |label| format!(
|
||||
"{}: ",
|
||||
label.ident,
|
||||
))
|
||||
);
|
||||
cx.emit_spanned_lint(WHILE_TRUE, condition_span, BuiltinWhileTrue {
|
||||
suggestion: condition_span,
|
||||
replace,
|
||||
});
|
||||
"{}loop",
|
||||
label.map_or_else(String::new, |label| format!("{}: ", label.ident,))
|
||||
);
|
||||
cx.emit_spanned_lint(
|
||||
WHILE_TRUE,
|
||||
condition_span,
|
||||
BuiltinWhileTrue { suggestion: condition_span, replace },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -164,7 +162,9 @@ declare_lint_pass!(BoxPointers => [BOX_POINTERS]);
|
|||
impl BoxPointers {
|
||||
fn check_heap_type(&self, cx: &LateContext<'_>, span: Span, ty: Ty<'_>) {
|
||||
for leaf in ty.walk() {
|
||||
if let GenericArgKind::Type(leaf_ty) = leaf.unpack() && leaf_ty.is_box() {
|
||||
if let GenericArgKind::Type(leaf_ty) = leaf.unpack()
|
||||
&& leaf_ty.is_box()
|
||||
{
|
||||
cx.emit_spanned_lint(BOX_POINTERS, span, BuiltinBoxPointers { ty });
|
||||
}
|
||||
}
|
||||
|
@ -681,7 +681,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
|
|||
// We shouldn't recommend implementing `Copy` on stateful things,
|
||||
// such as iterators.
|
||||
if let Some(iter_trait) = cx.tcx.get_diagnostic_item(sym::Iterator)
|
||||
&& cx.tcx
|
||||
&& cx
|
||||
.tcx
|
||||
.infer_ctxt()
|
||||
.build()
|
||||
.type_implements_trait(iter_trait, [ty], param_env)
|
||||
|
@ -1298,10 +1299,14 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
|
|||
// Now, check if the function has the `#[track_caller]` attribute
|
||||
&& let Some(attr) = cx.tcx.get_attr(def_id, sym::track_caller)
|
||||
{
|
||||
cx.emit_spanned_lint(UNGATED_ASYNC_FN_TRACK_CALLER, attr.span, BuiltinUngatedAsyncFnTrackCaller {
|
||||
label: span,
|
||||
parse_sess: &cx.tcx.sess.parse_sess,
|
||||
});
|
||||
cx.emit_spanned_lint(
|
||||
UNGATED_ASYNC_FN_TRACK_CALLER,
|
||||
attr.span,
|
||||
BuiltinUngatedAsyncFnTrackCaller {
|
||||
label: span,
|
||||
parse_sess: &cx.tcx.sess.parse_sess,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1342,7 +1342,7 @@ impl<'tcx> LateContext<'tcx> {
|
|||
&& let Some(init) = match parent_node {
|
||||
hir::Node::Expr(expr) => Some(expr),
|
||||
hir::Node::Local(hir::Local { init, .. }) => *init,
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
{
|
||||
expr = init.peel_blocks();
|
||||
|
@ -1391,9 +1391,9 @@ impl<'tcx> LateContext<'tcx> {
|
|||
hir::ItemKind::Const(.., body_id) | hir::ItemKind::Static(.., body_id) => {
|
||||
Some(self.tcx.hir().body(body_id).value)
|
||||
}
|
||||
_ => None
|
||||
}
|
||||
_ => None
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
{
|
||||
expr = init.peel_blocks();
|
||||
|
|
|
@ -75,14 +75,16 @@ impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
|
|||
&& supertraits(cx.tcx, t_principal.with_self_ty(cx.tcx, cx.tcx.types.trait_object_dummy_self))
|
||||
.any(|sup| sup.map_bound(|x| ty::ExistentialTraitRef::erase_self_ty(cx.tcx, x)) == target_principal)
|
||||
{
|
||||
let label = impl_.items.iter().find_map(|i| (i.ident.name == sym::Target).then_some(i.span)).map(|label| SupertraitAsDerefTargetLabel {
|
||||
label,
|
||||
});
|
||||
cx.emit_spanned_lint(DEREF_INTO_DYN_SUPERTRAIT, cx.tcx.def_span(item.owner_id.def_id), SupertraitAsDerefTarget {
|
||||
t,
|
||||
target_principal,
|
||||
label,
|
||||
});
|
||||
let label = impl_
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|i| (i.ident.name == sym::Target).then_some(i.span))
|
||||
.map(|label| SupertraitAsDerefTargetLabel { label });
|
||||
cx.emit_spanned_lint(
|
||||
DEREF_INTO_DYN_SUPERTRAIT,
|
||||
cx.tcx.def_span(item.owner_id.def_id),
|
||||
SupertraitAsDerefTarget { t, target_principal, label },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,18 +149,37 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
|
|||
let drop_is_single_call_in_arm = is_single_call_in_arm(cx, arg, expr);
|
||||
match fn_name {
|
||||
sym::mem_drop if arg_ty.is_ref() && !drop_is_single_call_in_arm => {
|
||||
cx.emit_spanned_lint(DROPPING_REFERENCES, expr.span, DropRefDiag { arg_ty, label: arg.span });
|
||||
},
|
||||
cx.emit_spanned_lint(
|
||||
DROPPING_REFERENCES,
|
||||
expr.span,
|
||||
DropRefDiag { arg_ty, label: arg.span },
|
||||
);
|
||||
}
|
||||
sym::mem_forget if arg_ty.is_ref() => {
|
||||
cx.emit_spanned_lint(FORGETTING_REFERENCES, expr.span, ForgetRefDiag { arg_ty, label: arg.span });
|
||||
},
|
||||
cx.emit_spanned_lint(
|
||||
FORGETTING_REFERENCES,
|
||||
expr.span,
|
||||
ForgetRefDiag { arg_ty, label: arg.span },
|
||||
);
|
||||
}
|
||||
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
|
||||
cx.emit_spanned_lint(DROPPING_COPY_TYPES, expr.span, DropCopyDiag { arg_ty, label: arg.span });
|
||||
cx.emit_spanned_lint(
|
||||
DROPPING_COPY_TYPES,
|
||||
expr.span,
|
||||
DropCopyDiag { arg_ty, label: arg.span },
|
||||
);
|
||||
}
|
||||
sym::mem_forget if is_copy => {
|
||||
cx.emit_spanned_lint(FORGETTING_COPY_TYPES, expr.span, ForgetCopyDiag { arg_ty, label: arg.span });
|
||||
cx.emit_spanned_lint(
|
||||
FORGETTING_COPY_TYPES,
|
||||
expr.span,
|
||||
ForgetCopyDiag { arg_ty, label: arg.span },
|
||||
);
|
||||
}
|
||||
sym::mem_drop if let ty::Adt(adt, _) = arg_ty.kind() && adt.is_manually_drop() => {
|
||||
sym::mem_drop
|
||||
if let ty::Adt(adt, _) = arg_ty.kind()
|
||||
&& adt.is_manually_drop() =>
|
||||
{
|
||||
cx.emit_spanned_lint(
|
||||
UNDROPPED_MANUALLY_DROPS,
|
||||
expr.span,
|
||||
|
@ -169,9 +188,9 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
|
|||
label: arg.span,
|
||||
suggestion: UndroppedManuallyDropsSuggestion {
|
||||
start_span: arg.span.shrink_to_lo(),
|
||||
end_span: arg.span.shrink_to_hi()
|
||||
}
|
||||
}
|
||||
end_span: arg.span.shrink_to_hi(),
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
_ => return,
|
||||
|
|
|
@ -59,13 +59,20 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
|
|||
_ => return,
|
||||
};
|
||||
|
||||
let sub = if let Some(recv) = extract_iterator_next_call(cx, arg)
|
||||
let sub = if let Some(recv) = extract_iterator_next_call(cx, arg)
|
||||
&& let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span)
|
||||
{
|
||||
ForLoopsOverFalliblesLoopSub::RemoveNext { suggestion: recv.span.between(arg.span.shrink_to_hi()), recv_snip }
|
||||
} else {
|
||||
ForLoopsOverFalliblesLoopSub::UseWhileLet { start_span: expr.span.with_hi(pat.span.lo()), end_span: pat.span.between(arg.span), var }
|
||||
} ;
|
||||
{
|
||||
ForLoopsOverFalliblesLoopSub::RemoveNext {
|
||||
suggestion: recv.span.between(arg.span.shrink_to_hi()),
|
||||
recv_snip,
|
||||
}
|
||||
} else {
|
||||
ForLoopsOverFalliblesLoopSub::UseWhileLet {
|
||||
start_span: expr.span.with_hi(pat.span.lo()),
|
||||
end_span: pat.span.between(arg.span),
|
||||
var,
|
||||
}
|
||||
};
|
||||
let question_mark = suggest_question_mark(cx, adt, args, expr.span)
|
||||
.then(|| ForLoopsOverFalliblesQuestionMark { suggestion: arg.span.shrink_to_hi() });
|
||||
let suggestion = ForLoopsOverFalliblesSuggestion {
|
||||
|
@ -84,13 +91,13 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
|
|||
|
||||
fn extract_for_loop<'tcx>(expr: &Expr<'tcx>) -> Option<(&'tcx Pat<'tcx>, &'tcx Expr<'tcx>)> {
|
||||
if let hir::ExprKind::DropTemps(e) = expr.kind
|
||||
&& let hir::ExprKind::Match(iterexpr, [arm], hir::MatchSource::ForLoopDesugar) = e.kind
|
||||
&& let hir::ExprKind::Call(_, [arg]) = iterexpr.kind
|
||||
&& let hir::ExprKind::Loop(block, ..) = arm.body.kind
|
||||
&& let [stmt] = block.stmts
|
||||
&& let hir::StmtKind::Expr(e) = stmt.kind
|
||||
&& let hir::ExprKind::Match(_, [_, some_arm], _) = e.kind
|
||||
&& let hir::PatKind::Struct(_, [field], _) = some_arm.pat.kind
|
||||
&& let hir::ExprKind::Match(iterexpr, [arm], hir::MatchSource::ForLoopDesugar) = e.kind
|
||||
&& let hir::ExprKind::Call(_, [arg]) = iterexpr.kind
|
||||
&& let hir::ExprKind::Loop(block, ..) = arm.body.kind
|
||||
&& let [stmt] = block.stmts
|
||||
&& let hir::StmtKind::Expr(e) = stmt.kind
|
||||
&& let hir::ExprKind::Match(_, [_, some_arm], _) = e.kind
|
||||
&& let hir::PatKind::Struct(_, [field], _) = some_arm.pat.kind
|
||||
{
|
||||
Some((field.pat, arg))
|
||||
} else {
|
||||
|
@ -104,11 +111,11 @@ fn extract_iterator_next_call<'tcx>(
|
|||
) -> Option<&'tcx Expr<'tcx>> {
|
||||
// This won't work for `Iterator::next(iter)`, is this an issue?
|
||||
if let hir::ExprKind::MethodCall(_, recv, _, _) = expr.kind
|
||||
&& cx.typeck_results().type_dependent_def_id(expr.hir_id) == cx.tcx.lang_items().next_fn()
|
||||
&& cx.typeck_results().type_dependent_def_id(expr.hir_id) == cx.tcx.lang_items().next_fn()
|
||||
{
|
||||
Some(recv)
|
||||
} else {
|
||||
return None
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -62,13 +62,11 @@ fn typeck_results_of_method_fn<'tcx>(
|
|||
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
|
||||
{
|
||||
Some((segment.ident.span, def_id, cx.typeck_results().node_args(expr.hir_id)))
|
||||
},
|
||||
_ => {
|
||||
match cx.typeck_results().node_type(expr.hir_id).kind() {
|
||||
&ty::FnDef(def_id, args) => Some((expr.span, def_id, args)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
_ => match cx.typeck_results().node_type(expr.hir_id).kind() {
|
||||
&ty::FnDef(def_id, args) => Some((expr.span, def_id, args)),
|
||||
_ => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,14 +132,11 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
|
|||
_: rustc_hir::HirId,
|
||||
) {
|
||||
if let Some(segment) = path.segments.iter().nth_back(1)
|
||||
&& lint_ty_kind_usage(cx, &segment.res)
|
||||
&& lint_ty_kind_usage(cx, &segment.res)
|
||||
{
|
||||
let span = path.span.with_hi(
|
||||
segment.args.map_or(segment.ident.span, |a| a.span_ext).hi()
|
||||
);
|
||||
cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindKind {
|
||||
suggestion: span,
|
||||
});
|
||||
let span =
|
||||
path.span.with_hi(segment.args.map_or(segment.ident.span, |a| a.span_ext).hi());
|
||||
cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindKind { suggestion: span });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,10 +161,7 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
|
|||
None
|
||||
}
|
||||
}
|
||||
Some(Node::Expr(Expr {
|
||||
kind: ExprKind::Path(qpath),
|
||||
..
|
||||
})) => {
|
||||
Some(Node::Expr(Expr { kind: ExprKind::Path(qpath), .. })) => {
|
||||
if let QPath::TypeRelative(qpath_ty, ..) = qpath
|
||||
&& qpath_ty.hir_id == ty.hir_id
|
||||
{
|
||||
|
@ -180,10 +172,7 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
|
|||
}
|
||||
// Can't unify these two branches because qpath below is `&&` and above is `&`
|
||||
// and `A | B` paths don't play well together with adjustments, apparently.
|
||||
Some(Node::Expr(Expr {
|
||||
kind: ExprKind::Struct(qpath, ..),
|
||||
..
|
||||
})) => {
|
||||
Some(Node::Expr(Expr { kind: ExprKind::Struct(qpath, ..), .. })) => {
|
||||
if let QPath::TypeRelative(qpath_ty, ..) = qpath
|
||||
&& qpath_ty.hir_id == ty.hir_id
|
||||
{
|
||||
|
@ -192,22 +181,28 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
|
|||
None
|
||||
}
|
||||
}
|
||||
_ => None
|
||||
_ => None,
|
||||
};
|
||||
|
||||
match span {
|
||||
Some(span) => {
|
||||
cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindKind {
|
||||
suggestion: span,
|
||||
});
|
||||
},
|
||||
cx.emit_spanned_lint(
|
||||
USAGE_OF_TY_TYKIND,
|
||||
path.span,
|
||||
TykindKind { suggestion: span },
|
||||
);
|
||||
}
|
||||
None => cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindDiag),
|
||||
}
|
||||
} else if !ty.span.from_expansion() && path.segments.len() > 1 && let Some(ty) = is_ty_or_ty_ctxt(cx, &path) {
|
||||
cx.emit_spanned_lint(USAGE_OF_QUALIFIED_TY, path.span, TyQualified {
|
||||
ty,
|
||||
suggestion: path.span,
|
||||
});
|
||||
} else if !ty.span.from_expansion()
|
||||
&& path.segments.len() > 1
|
||||
&& let Some(ty) = is_ty_or_ty_ctxt(cx, &path)
|
||||
{
|
||||
cx.emit_spanned_lint(
|
||||
USAGE_OF_QUALIFIED_TY,
|
||||
path.span,
|
||||
TyQualified { ty, suggestion: path.span },
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
@ -398,11 +393,11 @@ impl LateLintPass<'_> for Diagnostics {
|
|||
}
|
||||
|
||||
debug!(?parent);
|
||||
if let Node::Item(Item { kind: ItemKind::Impl(impl_), .. }) = parent &&
|
||||
let Impl { of_trait: Some(of_trait), .. } = impl_ &&
|
||||
let Some(def_id) = of_trait.trait_def_id() &&
|
||||
let Some(name) = cx.tcx.get_diagnostic_name(def_id) &&
|
||||
matches!(name, sym::IntoDiagnostic | sym::AddToDiagnostic | sym::DecorateLint)
|
||||
if let Node::Item(Item { kind: ItemKind::Impl(impl_), .. }) = parent
|
||||
&& let Impl { of_trait: Some(of_trait), .. } = impl_
|
||||
&& let Some(def_id) = of_trait.trait_def_id()
|
||||
&& let Some(name) = cx.tcx.get_diagnostic_name(def_id)
|
||||
&& matches!(name, sym::IntoDiagnostic | sym::AddToDiagnostic | sym::DecorateLint)
|
||||
{
|
||||
found_impl = true;
|
||||
break;
|
||||
|
@ -416,9 +411,9 @@ impl LateLintPass<'_> for Diagnostics {
|
|||
let mut found_diagnostic_message = false;
|
||||
for ty in args.types() {
|
||||
debug!(?ty);
|
||||
if let Some(adt_def) = ty.ty_adt_def() &&
|
||||
let Some(name) = cx.tcx.get_diagnostic_name(adt_def.did()) &&
|
||||
matches!(name, sym::DiagnosticMessage | sym::SubdiagnosticMessage)
|
||||
if let Some(adt_def) = ty.ty_adt_def()
|
||||
&& let Some(name) = cx.tcx.get_diagnostic_name(adt_def.did())
|
||||
&& matches!(name, sym::DiagnosticMessage | sym::SubdiagnosticMessage)
|
||||
{
|
||||
found_diagnostic_message = true;
|
||||
break;
|
||||
|
@ -486,8 +481,9 @@ impl EarlyLintPass for Diagnostics {
|
|||
}
|
||||
};
|
||||
if let ast::ExprKind::Lit(lit) = arg.kind
|
||||
&& let ast::token::LitKind::Str = lit.kind {
|
||||
true
|
||||
&& let ast::token::LitKind::Str = lit.kind
|
||||
{
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
@ -524,16 +520,19 @@ impl LateLintPass<'_> for BadOptAccess {
|
|||
}
|
||||
|
||||
for field in adt_def.all_fields() {
|
||||
if field.name == target.name &&
|
||||
let Some(attr) = cx.tcx.get_attr(field.did, sym::rustc_lint_opt_deny_field_access) &&
|
||||
let Some(items) = attr.meta_item_list() &&
|
||||
let Some(item) = items.first() &&
|
||||
let Some(lit) = item.lit() &&
|
||||
let ast::LitKind::Str(val, _) = lit.kind
|
||||
if field.name == target.name
|
||||
&& let Some(attr) =
|
||||
cx.tcx.get_attr(field.did, sym::rustc_lint_opt_deny_field_access)
|
||||
&& let Some(items) = attr.meta_item_list()
|
||||
&& let Some(item) = items.first()
|
||||
&& let Some(lit) = item.lit()
|
||||
&& let ast::LitKind::Str(val, _) = lit.kind
|
||||
{
|
||||
cx.emit_spanned_lint(BAD_OPT_ACCESS, expr.span, BadOptAccessDiag {
|
||||
msg: val.as_str(),
|
||||
});
|
||||
cx.emit_spanned_lint(
|
||||
BAD_OPT_ACCESS,
|
||||
expr.span,
|
||||
BadOptAccessDiag { msg: val.as_str() },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,8 +64,13 @@ impl<'tcx> LateLintPass<'tcx> for InvalidFromUtf8 {
|
|||
&& let ExprKind::Path(ref qpath) = path.kind
|
||||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
||||
&& let Some(diag_item) = cx.tcx.get_diagnostic_name(def_id)
|
||||
&& [sym::str_from_utf8, sym::str_from_utf8_mut,
|
||||
sym::str_from_utf8_unchecked, sym::str_from_utf8_unchecked_mut].contains(&diag_item)
|
||||
&& [
|
||||
sym::str_from_utf8,
|
||||
sym::str_from_utf8_mut,
|
||||
sym::str_from_utf8_unchecked,
|
||||
sym::str_from_utf8_unchecked_mut,
|
||||
]
|
||||
.contains(&diag_item)
|
||||
{
|
||||
let lint = |label, utf8_error: Utf8Error| {
|
||||
let method = diag_item.as_str().strip_prefix("str_").unwrap();
|
||||
|
@ -74,13 +79,17 @@ impl<'tcx> LateLintPass<'tcx> for InvalidFromUtf8 {
|
|||
let is_unchecked_variant = diag_item.as_str().contains("unchecked");
|
||||
|
||||
cx.emit_spanned_lint(
|
||||
if is_unchecked_variant { INVALID_FROM_UTF8_UNCHECKED } else { INVALID_FROM_UTF8 },
|
||||
if is_unchecked_variant {
|
||||
INVALID_FROM_UTF8_UNCHECKED
|
||||
} else {
|
||||
INVALID_FROM_UTF8
|
||||
},
|
||||
expr.span,
|
||||
if is_unchecked_variant {
|
||||
InvalidFromUtf8Diag::Unchecked { method, valid_up_to, label }
|
||||
} else {
|
||||
InvalidFromUtf8Diag::Checked { method, valid_up_to, label }
|
||||
}
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
|
@ -95,18 +104,19 @@ impl<'tcx> LateLintPass<'tcx> for InvalidFromUtf8 {
|
|||
{
|
||||
lint(init.span, utf8_error);
|
||||
}
|
||||
},
|
||||
}
|
||||
ExprKind::Array(args) => {
|
||||
let elements = args.iter().map(|e|{
|
||||
match &e.kind {
|
||||
let elements = args
|
||||
.iter()
|
||||
.map(|e| match &e.kind {
|
||||
ExprKind::Lit(Spanned { node: lit, .. }) => match lit {
|
||||
LitKind::Byte(b) => Some(*b),
|
||||
LitKind::Int(b, _) => Some(*b as u8),
|
||||
_ => None
|
||||
}
|
||||
_ => None
|
||||
}
|
||||
}).collect::<Option<Vec<_>>>();
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Option<Vec<_>>>();
|
||||
|
||||
if let Some(elements) = elements
|
||||
&& let Err(utf8_error) = std::str::from_utf8(&elements)
|
||||
|
|
|
@ -634,7 +634,9 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
|
|||
/// diagnostic with no change to `specs`.
|
||||
fn insert_spec(&mut self, id: LintId, (mut level, src): LevelAndSource) {
|
||||
let (old_level, old_src) = self.provider.get_lint_level(id.lint, &self.sess);
|
||||
if let Level::Expect(id) = &mut level && let LintExpectationId::Stable { .. } = id {
|
||||
if let Level::Expect(id) = &mut level
|
||||
&& let LintExpectationId::Stable { .. } = id
|
||||
{
|
||||
*id = id.normalize();
|
||||
}
|
||||
// Setting to a non-forbid level is an error if the lint previously had
|
||||
|
@ -706,7 +708,9 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
|
|||
// The lint `unfulfilled_lint_expectations` can't be expected, as it would suppress itself.
|
||||
// Handling expectations of this lint would add additional complexity with little to no
|
||||
// benefit. The expect level for this lint will therefore be ignored.
|
||||
if let Level::Expect(_) = level && id == LintId::of(UNFULFILLED_LINT_EXPECTATIONS) {
|
||||
if let Level::Expect(_) = level
|
||||
&& id == LintId::of(UNFULFILLED_LINT_EXPECTATIONS)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -747,8 +751,9 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
|
|||
None => continue,
|
||||
// This is the only lint level with a `LintExpectationId` that can be created from an attribute
|
||||
Some(Level::Expect(unstable_id)) if let Some(hir_id) = source_hir_id => {
|
||||
let LintExpectationId::Unstable { attr_id, lint_index } = unstable_id
|
||||
else { bug!("stable id Level::from_attr") };
|
||||
let LintExpectationId::Unstable { attr_id, lint_index } = unstable_id else {
|
||||
bug!("stable id Level::from_attr")
|
||||
};
|
||||
|
||||
let stable_id = LintExpectationId::Stable {
|
||||
hir_id,
|
||||
|
|
|
@ -42,18 +42,17 @@ impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable {
|
|||
if let hir::ItemKind::Trait(_, _, _, _, _) = item.kind
|
||||
&& cx.tcx.object_safety_violations(def_id).is_empty()
|
||||
{
|
||||
let direct_super_traits_iter = cx.tcx
|
||||
.super_predicates_of(def_id)
|
||||
.predicates
|
||||
.into_iter()
|
||||
.filter_map(|(pred, _)| pred.as_trait_clause());
|
||||
let direct_super_traits_iter = cx
|
||||
.tcx
|
||||
.super_predicates_of(def_id)
|
||||
.predicates
|
||||
.into_iter()
|
||||
.filter_map(|(pred, _)| pred.as_trait_clause());
|
||||
if direct_super_traits_iter.count() > 1 {
|
||||
cx.emit_spanned_lint(
|
||||
MULTIPLE_SUPERTRAIT_UPCASTABLE,
|
||||
cx.tcx.def_span(def_id),
|
||||
crate::lints::MultipleSupertraitUpcastable {
|
||||
ident: item.ident
|
||||
},
|
||||
crate::lints::MultipleSupertraitUpcastable { ident: item.ident },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -511,7 +511,9 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
|
|||
}
|
||||
|
||||
fn check_impl_item(&mut self, cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) {
|
||||
if let hir::ImplItemKind::Const(..) = ii.kind && !assoc_item_in_trait_impl(cx, ii) {
|
||||
if let hir::ImplItemKind::Const(..) = ii.kind
|
||||
&& !assoc_item_in_trait_impl(cx, ii)
|
||||
{
|
||||
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ii.ident);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,22 +46,26 @@ fn incorrect_check<'a, 'tcx: 'a>(
|
|||
if let ExprKind::MethodCall(_, _expr, [], _) = e.kind
|
||||
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
||||
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)
|
||||
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id) {
|
||||
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
|
||||
{
|
||||
return Some(PtrNullChecksDiag::FnRet { fn_name });
|
||||
} else if let ExprKind::Call(path, _args) = e.kind
|
||||
&& let ExprKind::Path(ref qpath) = path.kind
|
||||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
||||
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)
|
||||
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id) {
|
||||
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
|
||||
{
|
||||
return Some(PtrNullChecksDiag::FnRet { fn_name });
|
||||
}
|
||||
e = if let ExprKind::Cast(expr, t) = e.kind
|
||||
&& let TyKind::Ptr(_) = t.kind {
|
||||
&& let TyKind::Ptr(_) = t.kind
|
||||
{
|
||||
had_at_least_one_cast = true;
|
||||
expr
|
||||
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
|
||||
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
||||
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::ptr_cast | sym::ptr_cast_mut)) {
|
||||
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::ptr_cast | sym::ptr_cast_mut))
|
||||
{
|
||||
had_at_least_one_cast = true;
|
||||
expr
|
||||
} else if had_at_least_one_cast {
|
||||
|
@ -127,10 +131,11 @@ impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
|
|||
// (fn_ptr as *<const/mut> <ty>) == (0 as <ty>)
|
||||
ExprKind::Cast(cast_expr, _)
|
||||
if let ExprKind::Lit(spanned) = cast_expr.kind
|
||||
&& let LitKind::Int(v, _) = spanned.node && v == 0 =>
|
||||
&& let LitKind::Int(v, _) = spanned.node
|
||||
&& v == 0 =>
|
||||
{
|
||||
cx.emit_spanned_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag)
|
||||
},
|
||||
}
|
||||
|
||||
// Catching:
|
||||
// (fn_ptr as *<const/mut> <ty>) == std::ptr::null()
|
||||
|
@ -141,9 +146,9 @@ impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
|
|||
&& (diag_item == sym::ptr_null || diag_item == sym::ptr_null_mut) =>
|
||||
{
|
||||
cx.emit_spanned_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag)
|
||||
},
|
||||
}
|
||||
|
||||
_ => {},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
@ -93,7 +93,10 @@ fn is_operation_we_care_about<'tcx>(
|
|||
if let ExprKind::Call(path, [arg_ptr, _arg_val]) = e.kind
|
||||
&& let ExprKind::Path(ref qpath) = path.kind
|
||||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
||||
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::ptr_write | sym::ptr_write_volatile | sym::ptr_write_unaligned))
|
||||
&& matches!(
|
||||
cx.tcx.get_diagnostic_name(def_id),
|
||||
Some(sym::ptr_write | sym::ptr_write_volatile | sym::ptr_write_unaligned)
|
||||
)
|
||||
{
|
||||
Some((true, arg_ptr))
|
||||
} else {
|
||||
|
|
|
@ -163,8 +163,10 @@ fn lint_overflowing_range_endpoint<'tcx>(
|
|||
ty: &str,
|
||||
) -> bool {
|
||||
// Look past casts to support cases like `0..256 as u8`
|
||||
let (expr, lit_span) = if let Node::Expr(par_expr) = cx.tcx.hir().get(cx.tcx.hir().parent_id(expr.hir_id))
|
||||
&& let ExprKind::Cast(_, _) = par_expr.kind {
|
||||
let (expr, lit_span) = if let Node::Expr(par_expr) =
|
||||
cx.tcx.hir().get(cx.tcx.hir().parent_id(expr.hir_id))
|
||||
&& let ExprKind::Cast(_, _) = par_expr.kind
|
||||
{
|
||||
(par_expr, expr.span)
|
||||
} else {
|
||||
(expr, expr.span)
|
||||
|
@ -580,8 +582,8 @@ fn lint_nan<'tcx>(
|
|||
) -> InvalidNanComparisons {
|
||||
// FIXME(#72505): This suggestion can be restored if `f{32,64}::is_nan` is made const.
|
||||
let suggestion = (!cx.tcx.hir().is_inside_const_context(e.hir_id)).then(|| {
|
||||
if let Some(l_span) = l.span.find_ancestor_inside(e.span) &&
|
||||
let Some(r_span) = r.span.find_ancestor_inside(e.span)
|
||||
if let Some(l_span) = l.span.find_ancestor_inside(e.span)
|
||||
&& let Some(r_span) = r.span.find_ancestor_inside(e.span)
|
||||
{
|
||||
f(l_span, r_span)
|
||||
} else {
|
||||
|
@ -1293,11 +1295,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
|
|||
CItemKind::Definition => "fn",
|
||||
};
|
||||
let span_note = if let ty::Adt(def, _) = ty.kind()
|
||||
&& let Some(sp) = self.cx.tcx.hir().span_if_local(def.did()) {
|
||||
Some(sp)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
&& let Some(sp) = self.cx.tcx.hir().span_if_local(def.did())
|
||||
{
|
||||
Some(sp)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.cx.emit_spanned_lint(
|
||||
lint,
|
||||
sp,
|
||||
|
@ -1460,7 +1463,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
|
|||
type BreakTy = Ty<'tcx>;
|
||||
|
||||
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
||||
if let ty::FnPtr(sig) = ty.kind() && !self.visitor.is_internal_abi(sig.abi()) {
|
||||
if let ty::FnPtr(sig) = ty.kind()
|
||||
&& !self.visitor.is_internal_abi(sig.abi())
|
||||
{
|
||||
self.tys.push(ty);
|
||||
}
|
||||
|
||||
|
@ -1734,7 +1739,8 @@ impl InvalidAtomicOrdering {
|
|||
}
|
||||
|
||||
fn check_atomic_load_store(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
||||
if let Some((method, args)) = Self::inherent_atomic_method_call(cx, expr, &[sym::load, sym::store])
|
||||
if let Some((method, args)) =
|
||||
Self::inherent_atomic_method_call(cx, expr, &[sym::load, sym::store])
|
||||
&& let Some((ordering_arg, invalid_ordering)) = match method {
|
||||
sym::load => Some((&args[0], sym::Release)),
|
||||
sym::store => Some((&args[1], sym::Acquire)),
|
||||
|
@ -1744,9 +1750,17 @@ impl InvalidAtomicOrdering {
|
|||
&& (ordering == invalid_ordering || ordering == sym::AcqRel)
|
||||
{
|
||||
if method == sym::load {
|
||||
cx.emit_spanned_lint(INVALID_ATOMIC_ORDERING, ordering_arg.span, AtomicOrderingLoad);
|
||||
cx.emit_spanned_lint(
|
||||
INVALID_ATOMIC_ORDERING,
|
||||
ordering_arg.span,
|
||||
AtomicOrderingLoad,
|
||||
);
|
||||
} else {
|
||||
cx.emit_spanned_lint(INVALID_ATOMIC_ORDERING, ordering_arg.span, AtomicOrderingStore);
|
||||
cx.emit_spanned_lint(
|
||||
INVALID_ATOMIC_ORDERING,
|
||||
ordering_arg.span,
|
||||
AtomicOrderingStore,
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -782,21 +782,23 @@ trait UnusedDelimLint {
|
|||
};
|
||||
let suggestion = spans.map(|(lo, hi)| {
|
||||
let sm = cx.sess().source_map();
|
||||
let lo_replace =
|
||||
if (keep_space.0 || is_kw) &&
|
||||
let Ok(snip) = sm.span_to_prev_source(lo) && !snip.ends_with(' ') {
|
||||
" "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let lo_replace = if (keep_space.0 || is_kw)
|
||||
&& let Ok(snip) = sm.span_to_prev_source(lo)
|
||||
&& !snip.ends_with(' ')
|
||||
{
|
||||
" "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
let hi_replace =
|
||||
if keep_space.1 &&
|
||||
let Ok(snip) = sm.span_to_next_source(hi) && !snip.starts_with(' ') {
|
||||
" "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let hi_replace = if keep_space.1
|
||||
&& let Ok(snip) = sm.span_to_next_source(hi)
|
||||
&& !snip.starts_with(' ')
|
||||
{
|
||||
" "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
UnusedDelimSuggestion {
|
||||
start_span: lo,
|
||||
start_replace: lo_replace,
|
||||
|
@ -1056,10 +1058,10 @@ impl UnusedParens {
|
|||
impl EarlyLintPass for UnusedParens {
|
||||
#[inline]
|
||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
|
||||
if let ExprKind::Binary(op, lhs, _rhs) = &e.kind &&
|
||||
(op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl) &&
|
||||
let ExprKind::Cast(_expr, ty) = &lhs.kind &&
|
||||
let ast::TyKind::Paren(_) = &ty.kind
|
||||
if let ExprKind::Binary(op, lhs, _rhs) = &e.kind
|
||||
&& (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl)
|
||||
&& let ExprKind::Cast(_expr, ty) = &lhs.kind
|
||||
&& let ast::TyKind::Paren(_) = &ty.kind
|
||||
{
|
||||
self.parens_in_cast_in_lt.push(ty.id);
|
||||
}
|
||||
|
@ -1111,13 +1113,19 @@ impl EarlyLintPass for UnusedParens {
|
|||
}
|
||||
|
||||
fn check_expr_post(&mut self, _cx: &EarlyContext<'_>, e: &ast::Expr) {
|
||||
if let ExprKind::Binary(op, lhs, _rhs) = &e.kind &&
|
||||
(op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl) &&
|
||||
let ExprKind::Cast(_expr, ty) = &lhs.kind &&
|
||||
let ast::TyKind::Paren(_) = &ty.kind
|
||||
if let ExprKind::Binary(op, lhs, _rhs) = &e.kind
|
||||
&& (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl)
|
||||
&& let ExprKind::Cast(_expr, ty) = &lhs.kind
|
||||
&& let ast::TyKind::Paren(_) = &ty.kind
|
||||
{
|
||||
let id = self.parens_in_cast_in_lt.pop().expect("check_expr and check_expr_post must balance");
|
||||
assert_eq!(id, ty.id, "check_expr, check_ty, and check_expr_post are called, in that order, by the visitor");
|
||||
let id = self
|
||||
.parens_in_cast_in_lt
|
||||
.pop()
|
||||
.expect("check_expr and check_expr_post must balance");
|
||||
assert_eq!(
|
||||
id, ty.id,
|
||||
"check_expr, check_ty, and check_expr_post are called, in that order, by the visitor"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1161,8 +1169,8 @@ impl EarlyLintPass for UnusedParens {
|
|||
}
|
||||
|
||||
fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
|
||||
if let ast::TyKind::Paren(_) = ty.kind &&
|
||||
Some(&ty.id) == self.parens_in_cast_in_lt.last()
|
||||
if let ast::TyKind::Paren(_) = ty.kind
|
||||
&& Some(&ty.id) == self.parens_in_cast_in_lt.last()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
@ -1206,13 +1214,14 @@ impl EarlyLintPass for UnusedParens {
|
|||
fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
|
||||
use rustc_ast::{WhereBoundPredicate, WherePredicate};
|
||||
if let WherePredicate::BoundPredicate(WhereBoundPredicate {
|
||||
bounded_ty,
|
||||
bound_generic_params,
|
||||
..
|
||||
}) = pred &&
|
||||
let ast::TyKind::Paren(_) = &bounded_ty.kind &&
|
||||
bound_generic_params.is_empty() {
|
||||
self.with_self_ty_parens = true;
|
||||
bounded_ty,
|
||||
bound_generic_params,
|
||||
..
|
||||
}) = pred
|
||||
&& let ast::TyKind::Paren(_) = &bounded_ty.kind
|
||||
&& bound_generic_params.is_empty()
|
||||
{
|
||||
self.with_self_ty_parens = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1516,9 +1525,8 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
|
|||
match e.kind {
|
||||
hir::ExprKind::Call(path_expr, [_])
|
||||
if let hir::ExprKind::Path(qpath) = &path_expr.kind
|
||||
&& let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()
|
||||
&& cx.tcx.is_diagnostic_item(sym::box_new, did)
|
||||
=> {}
|
||||
&& let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()
|
||||
&& cx.tcx.is_diagnostic_item(sym::box_new, did) => {}
|
||||
_ => return,
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue