1
Fork 0

Move local functions to outer scope.

This commit is contained in:
Aaron Kofsky 2022-06-04 19:52:12 -04:00
parent 6b179e3a67
commit a7e2b3e879

View file

@ -175,13 +175,15 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
})
}
}
}
}
fn build_and_emit_lint(
fn build_and_emit_lint(
lint: LintDiagnosticBuilder<'_, ()>,
local: &hir::Local<'_>,
init_span: rustc_span::Span,
msg: &str,
) {
) {
lint.build(msg)
.span_suggestion_verbose(
local.pat.span,
@ -196,10 +198,10 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
Applicability::HasPlaceholders,
)
.emit();
}
}
// return true if `ty` is a type that is marked as `must_use`
fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
// return true if `ty` is a type that is marked as `must_use`
fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
match ty.kind() {
ty::Adt(adt, _) => has_must_use_attr(cx, adt.did()),
ty::Foreign(ref did) => has_must_use_attr(cx, *did),
@ -214,9 +216,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
ty::Tuple(substs) => substs.iter().any(|ty| is_must_use_ty(cx, ty)),
ty::Opaque(ref def_id, _) => {
for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) {
if let ty::PredicateKind::Trait(trait_predicate) =
predicate.kind().skip_binder()
{
if let ty::PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() {
if has_must_use_attr(cx, trait_predicate.trait_ref.def_id) {
return true;
}
@ -226,9 +226,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
}
ty::Dynamic(binder, _) => {
for predicate in binder.iter() {
if let ty::ExistentialPredicate::Trait(ref trait_ref) =
predicate.skip_binder()
{
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
if has_must_use_attr(cx, trait_ref.def_id) {
return true;
}
@ -238,10 +236,10 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
}
_ => false,
}
}
}
// check if expr is calling method or function with #[must_use] attribute
fn is_must_use_func_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
// check if expr is calling method or function with #[must_use] attribute
fn is_must_use_func_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
let did = match expr.kind {
hir::ExprKind::Call(path, _) if let hir::ExprKind::Path(ref qpath) = path.kind => {
if let hir::def::Res::Def(_, did) = cx.qpath_res(qpath, path.hir_id) {
@ -257,14 +255,12 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
};
did.map_or(false, |did| has_must_use_attr(cx, did))
}
}
// returns true if DefId contains a `#[must_use]` attribute
fn has_must_use_attr(cx: &LateContext<'_>, did: hir::def_id::DefId) -> bool {
// returns true if DefId contains a `#[must_use]` attribute
fn has_must_use_attr(cx: &LateContext<'_>, did: hir::def_id::DefId) -> bool {
cx.tcx
.get_attrs(did, rustc_span::sym::must_use)
.find(|a| a.has_name(rustc_span::sym::must_use))
.is_some()
}
}
}