Skip to content

Commit a7e2b3e

Browse files
committed
Move local functions to outer scope.
1 parent 6b179e3 commit a7e2b3e

File tree

1 file changed

+65
-69
lines changed

1 file changed

+65
-69
lines changed

compiler/rustc_lint/src/let_underscore.rs

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -175,74 +175,72 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
175175
})
176176
}
177177
}
178+
}
179+
}
178180

179-
fn build_and_emit_lint(
180-
lint: LintDiagnosticBuilder<'_, ()>,
181-
local: &hir::Local<'_>,
182-
init_span: rustc_span::Span,
183-
msg: &str,
184-
) {
185-
lint.build(msg)
186-
.span_suggestion_verbose(
187-
local.pat.span,
188-
"consider binding to an unused variable",
189-
"_unused",
190-
Applicability::MachineApplicable,
191-
)
192-
.span_suggestion_verbose(
193-
init_span,
194-
"consider explicitly droping with `std::mem::drop`",
195-
"drop(...)",
196-
Applicability::HasPlaceholders,
197-
)
198-
.emit();
199-
}
181+
fn build_and_emit_lint(
182+
lint: LintDiagnosticBuilder<'_, ()>,
183+
local: &hir::Local<'_>,
184+
init_span: rustc_span::Span,
185+
msg: &str,
186+
) {
187+
lint.build(msg)
188+
.span_suggestion_verbose(
189+
local.pat.span,
190+
"consider binding to an unused variable",
191+
"_unused",
192+
Applicability::MachineApplicable,
193+
)
194+
.span_suggestion_verbose(
195+
init_span,
196+
"consider explicitly droping with `std::mem::drop`",
197+
"drop(...)",
198+
Applicability::HasPlaceholders,
199+
)
200+
.emit();
201+
}
200202

201-
// return true if `ty` is a type that is marked as `must_use`
202-
fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
203-
match ty.kind() {
204-
ty::Adt(adt, _) => has_must_use_attr(cx, adt.did()),
205-
ty::Foreign(ref did) => has_must_use_attr(cx, *did),
206-
ty::Slice(ty)
207-
| ty::Array(ty, _)
208-
| ty::RawPtr(ty::TypeAndMut { ty, .. })
209-
| ty::Ref(_, ty, _) => {
210-
// for the Array case we don't need to care for the len == 0 case
211-
// because we don't want to lint functions returning empty arrays
212-
is_must_use_ty(cx, *ty)
213-
}
214-
ty::Tuple(substs) => substs.iter().any(|ty| is_must_use_ty(cx, ty)),
215-
ty::Opaque(ref def_id, _) => {
216-
for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) {
217-
if let ty::PredicateKind::Trait(trait_predicate) =
218-
predicate.kind().skip_binder()
219-
{
220-
if has_must_use_attr(cx, trait_predicate.trait_ref.def_id) {
221-
return true;
222-
}
223-
}
203+
// return true if `ty` is a type that is marked as `must_use`
204+
fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
205+
match ty.kind() {
206+
ty::Adt(adt, _) => has_must_use_attr(cx, adt.did()),
207+
ty::Foreign(ref did) => has_must_use_attr(cx, *did),
208+
ty::Slice(ty)
209+
| ty::Array(ty, _)
210+
| ty::RawPtr(ty::TypeAndMut { ty, .. })
211+
| ty::Ref(_, ty, _) => {
212+
// for the Array case we don't need to care for the len == 0 case
213+
// because we don't want to lint functions returning empty arrays
214+
is_must_use_ty(cx, *ty)
215+
}
216+
ty::Tuple(substs) => substs.iter().any(|ty| is_must_use_ty(cx, ty)),
217+
ty::Opaque(ref def_id, _) => {
218+
for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) {
219+
if let ty::PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() {
220+
if has_must_use_attr(cx, trait_predicate.trait_ref.def_id) {
221+
return true;
224222
}
225-
false
226223
}
227-
ty::Dynamic(binder, _) => {
228-
for predicate in binder.iter() {
229-
if let ty::ExistentialPredicate::Trait(ref trait_ref) =
230-
predicate.skip_binder()
231-
{
232-
if has_must_use_attr(cx, trait_ref.def_id) {
233-
return true;
234-
}
235-
}
224+
}
225+
false
226+
}
227+
ty::Dynamic(binder, _) => {
228+
for predicate in binder.iter() {
229+
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
230+
if has_must_use_attr(cx, trait_ref.def_id) {
231+
return true;
236232
}
237-
false
238233
}
239-
_ => false,
240234
}
235+
false
241236
}
237+
_ => false,
238+
}
239+
}
242240

243-
// check if expr is calling method or function with #[must_use] attribute
244-
fn is_must_use_func_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
245-
let did = match expr.kind {
241+
// check if expr is calling method or function with #[must_use] attribute
242+
fn is_must_use_func_call(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
243+
let did = match expr.kind {
246244
hir::ExprKind::Call(path, _) if let hir::ExprKind::Path(ref qpath) = path.kind => {
247245
if let hir::def::Res::Def(_, did) = cx.qpath_res(qpath, path.hir_id) {
248246
Some(did)
@@ -256,15 +254,13 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
256254
_ => None,
257255
};
258256

259-
did.map_or(false, |did| has_must_use_attr(cx, did))
260-
}
257+
did.map_or(false, |did| has_must_use_attr(cx, did))
258+
}
261259

262-
// returns true if DefId contains a `#[must_use]` attribute
263-
fn has_must_use_attr(cx: &LateContext<'_>, did: hir::def_id::DefId) -> bool {
264-
cx.tcx
265-
.get_attrs(did, rustc_span::sym::must_use)
266-
.find(|a| a.has_name(rustc_span::sym::must_use))
267-
.is_some()
268-
}
269-
}
260+
// returns true if DefId contains a `#[must_use]` attribute
261+
fn has_must_use_attr(cx: &LateContext<'_>, did: hir::def_id::DefId) -> bool {
262+
cx.tcx
263+
.get_attrs(did, rustc_span::sym::must_use)
264+
.find(|a| a.has_name(rustc_span::sym::must_use))
265+
.is_some()
270266
}

0 commit comments

Comments
 (0)