Skip to content

Commit c341b9b

Browse files
committed
Code cleanup
1 parent f82f58a commit c341b9b

File tree

3 files changed

+29
-40
lines changed

3 files changed

+29
-40
lines changed

clippy_lints/src/dereference.rs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use clippy_utils::{get_parent_expr, is_lint_allowed, path_to_local, walk_to_expr
66
use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
77
use rustc_data_structures::fx::FxIndexMap;
88
use rustc_errors::Applicability;
9+
use rustc_hir::intravisit::{walk_ty, Visitor};
910
use rustc_hir::{
10-
self as hir, BindingAnnotation, Body, BodyId, BorrowKind, Expr, ExprKind, FnRetTy, GenericArg, HirId, ImplItem,
11+
self as hir, BindingAnnotation, Body, BodyId, BorrowKind, Expr, ExprKind, GenericArg, HirId, ImplItem,
1112
ImplItemKind, Item, ItemKind, Local, MatchSource, Mutability, Node, Pat, PatKind, Path, QPath, TraitItem,
1213
TraitItemKind, TyKind, UnOp,
1314
};
@@ -870,38 +871,32 @@ fn binding_ty_auto_deref_stability(ty: &hir::Ty<'_>, precedence: i8) -> Position
870871
// Checks whether a type is inferred at some point.
871872
// e.g. `_`, `Box<_>`, `[_]`
872873
fn ty_contains_infer(ty: &hir::Ty<'_>) -> bool {
873-
match &ty.kind {
874-
TyKind::Slice(ty) | TyKind::Array(ty, _) => ty_contains_infer(ty),
875-
TyKind::Ptr(ty) | TyKind::Rptr(_, ty) => ty_contains_infer(ty.ty),
876-
TyKind::Tup(tys) => tys.iter().any(ty_contains_infer),
877-
TyKind::BareFn(ty) => {
878-
if ty.decl.inputs.iter().any(ty_contains_infer) {
879-
return true;
880-
}
881-
if let FnRetTy::Return(ty) = &ty.decl.output {
882-
ty_contains_infer(ty)
874+
struct V(bool);
875+
impl Visitor<'_> for V {
876+
fn visit_ty(&mut self, ty: &hir::Ty<'_>) {
877+
if self.0
878+
|| matches!(
879+
ty.kind,
880+
TyKind::OpaqueDef(..) | TyKind::Infer | TyKind::Typeof(_) | TyKind::Err
881+
)
882+
{
883+
self.0 = true;
883884
} else {
884-
false
885+
walk_ty(self, ty);
885886
}
886-
},
887-
&TyKind::Path(
888-
QPath::TypeRelative(_, path)
889-
| QPath::Resolved(
890-
_,
891-
Path {
892-
segments: [.., path], ..
893-
},
894-
),
895-
) => path.args.map_or(false, |args| {
896-
args.args.iter().any(|arg| match arg {
897-
GenericArg::Infer(_) => true,
898-
GenericArg::Type(ty) => ty_contains_infer(ty),
899-
_ => false,
900-
})
901-
}),
902-
TyKind::Path(_) | TyKind::OpaqueDef(..) | TyKind::Infer | TyKind::Typeof(_) | TyKind::Err => true,
903-
TyKind::Never | TyKind::TraitObject(..) => false,
887+
}
888+
889+
fn visit_generic_arg(&mut self, arg: &GenericArg<'_>) {
890+
if self.0 || matches!(arg, GenericArg::Infer(_)) {
891+
self.0 = true;
892+
} else if let GenericArg::Type(ty) = arg {
893+
self.visit_ty(ty);
894+
}
895+
}
904896
}
897+
let mut v = V(false);
898+
v.visit_ty(ty);
899+
v.0
905900
}
906901

907902
// Checks whether a type is stable when switching to auto dereferencing,
@@ -951,7 +946,7 @@ fn param_auto_deref_stability(ty: Ty<'_>, precedence: i8) -> Position {
951946

952947
fn ty_contains_field(ty: Ty<'_>, name: Symbol) -> bool {
953948
if let ty::Adt(adt, _) = *ty.kind() {
954-
adt.is_struct() && adt.non_enum_variant().fields.iter().any(|f| f.name == name)
949+
adt.is_struct() && adt.all_fields().any(|f| f.name == name)
955950
} else {
956951
false
957952
}

clippy_utils/src/lib.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,19 +2215,12 @@ pub fn walk_to_expr_usage<'tcx, T>(
22152215
_ => return None,
22162216
};
22172217
match parent.kind {
2218-
ExprKind::If(child, ..) | ExprKind::Match(child, ..) if child.hir_id != child_id => {
2219-
child_id = parent_id;
2220-
continue;
2221-
},
2218+
ExprKind::If(child, ..) | ExprKind::Match(child, ..) if child.hir_id != child_id => child_id = parent_id,
22222219
ExprKind::Break(Destination { target_id: Ok(id), .. }, _) => {
22232220
child_id = id;
22242221
iter = map.parent_iter(id);
2225-
continue;
2226-
},
2227-
ExprKind::Block(..) => {
2228-
child_id = parent_id;
2229-
continue;
22302222
},
2223+
ExprKind::Block(..) => child_id = parent_id,
22312224
_ => return None,
22322225
}
22332226
}

clippy_utils/src/ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ pub fn is_c_void(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
747747
}
748748
}
749749

750+
/// Gets the struct or enum variant from the given `Res`
750751
pub fn variant_of_res<'tcx>(cx: &LateContext<'tcx>, res: Res) -> Option<&'tcx VariantDef> {
751752
match res {
752753
Res::Def(DefKind::Struct, id) => Some(cx.tcx.adt_def(id).non_enum_variant()),

0 commit comments

Comments
 (0)