mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-22 18:15:07 +03:00
optimize allow unwrap types
This commit is contained in:
@@ -4761,6 +4761,8 @@ pub struct Methods {
|
||||
allowed_dotfiles: FxHashSet<&'static str>,
|
||||
format_args: FormatArgsStorage,
|
||||
allow_unwrap_types: Vec<String>,
|
||||
unwrap_allowed_ids: FxHashSet<rustc_hir::def_id::DefId>,
|
||||
unwrap_allowed_aliases: Vec<rustc_hir::def_id::DefId>,
|
||||
}
|
||||
|
||||
impl Methods {
|
||||
@@ -4778,6 +4780,8 @@ pub fn new(conf: &'static Conf, format_args: FormatArgsStorage) -> Self {
|
||||
allowed_dotfiles,
|
||||
format_args,
|
||||
allow_unwrap_types: conf.allow_unwrap_types.clone(),
|
||||
unwrap_allowed_ids: FxHashSet::default(),
|
||||
unwrap_allowed_aliases: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4953,6 +4957,19 @@ pub fn method_call<'tcx>(recv: &'tcx Expr<'tcx>) -> Option<(Symbol, &'tcx Expr<'
|
||||
}
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for Methods {
|
||||
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
|
||||
for s in &self.allow_unwrap_types {
|
||||
let def_ids = clippy_utils::paths::lookup_path_str(cx.tcx, clippy_utils::paths::PathNS::Type, s);
|
||||
for def_id in def_ids {
|
||||
if cx.tcx.def_kind(def_id) == rustc_hir::def::DefKind::TyAlias {
|
||||
self.unwrap_allowed_aliases.push(def_id);
|
||||
} else {
|
||||
self.unwrap_allowed_ids.insert(def_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
if expr.span.from_expansion() {
|
||||
return;
|
||||
@@ -4976,7 +4993,8 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
self.allow_expect_in_tests,
|
||||
self.allow_unwrap_in_consts,
|
||||
self.allow_expect_in_consts,
|
||||
&self.allow_unwrap_types,
|
||||
&self.unwrap_allowed_ids,
|
||||
&self.unwrap_allowed_aliases,
|
||||
);
|
||||
},
|
||||
ExprKind::MethodCall(..) => {
|
||||
@@ -5730,7 +5748,8 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
false,
|
||||
self.allow_expect_in_consts,
|
||||
self.allow_expect_in_tests,
|
||||
&self.allow_unwrap_types,
|
||||
&self.unwrap_allowed_ids,
|
||||
&self.unwrap_allowed_aliases,
|
||||
unwrap_expect_used::Variant::Expect,
|
||||
);
|
||||
expect_fun_call::check(cx, &self.format_args, expr, method_span, recv, arg);
|
||||
@@ -5743,7 +5762,8 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
true,
|
||||
self.allow_expect_in_consts,
|
||||
self.allow_expect_in_tests,
|
||||
&self.allow_unwrap_types,
|
||||
&self.unwrap_allowed_ids,
|
||||
&self.unwrap_allowed_aliases,
|
||||
unwrap_expect_used::Variant::Expect,
|
||||
);
|
||||
},
|
||||
@@ -5764,7 +5784,8 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
false,
|
||||
self.allow_unwrap_in_consts,
|
||||
self.allow_unwrap_in_tests,
|
||||
&self.allow_unwrap_types,
|
||||
&self.unwrap_allowed_ids,
|
||||
&self.unwrap_allowed_aliases,
|
||||
unwrap_expect_used::Variant::Unwrap,
|
||||
);
|
||||
},
|
||||
@@ -5776,7 +5797,8 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
true,
|
||||
self.allow_unwrap_in_consts,
|
||||
self.allow_unwrap_in_tests,
|
||||
&self.allow_unwrap_types,
|
||||
&self.unwrap_allowed_ids,
|
||||
&self.unwrap_allowed_aliases,
|
||||
unwrap_expect_used::Variant::Unwrap,
|
||||
);
|
||||
},
|
||||
|
||||
@@ -44,7 +44,8 @@ pub(super) fn check(
|
||||
is_err: bool,
|
||||
allow_unwrap_in_consts: bool,
|
||||
allow_unwrap_in_tests: bool,
|
||||
allow_unwrap_types: &[String],
|
||||
unwrap_allowed_ids: &rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::DefId>,
|
||||
unwrap_allowed_aliases: &[rustc_hir::def_id::DefId],
|
||||
variant: Variant,
|
||||
) {
|
||||
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
|
||||
@@ -66,51 +67,39 @@ pub(super) fn check(
|
||||
|
||||
let method_suffix = if is_err { "_err" } else { "" };
|
||||
|
||||
let ty_name = ty.to_string();
|
||||
if allow_unwrap_types
|
||||
.iter()
|
||||
.any(|allowed_type| ty_name.starts_with(allowed_type) || ty_name == *allowed_type)
|
||||
if let ty::Adt(adt, _) = ty.kind()
|
||||
&& unwrap_allowed_ids.contains(&adt.did())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for s in allow_unwrap_types {
|
||||
let def_ids = clippy_utils::paths::lookup_path_str(cx.tcx, clippy_utils::paths::PathNS::Type, s);
|
||||
for def_id in def_ids {
|
||||
if let ty::Adt(adt, _) = ty.kind()
|
||||
&& adt.did() == def_id
|
||||
{
|
||||
return;
|
||||
}
|
||||
if cx.tcx.def_kind(def_id) == DefKind::TyAlias {
|
||||
let alias_ty = cx.tcx.type_of(def_id).instantiate_identity();
|
||||
if let (ty::Adt(adt, substs), ty::Adt(alias_adt, alias_substs)) = (ty.kind(), alias_ty.kind())
|
||||
&& adt.did() == alias_adt.did()
|
||||
{
|
||||
let mut all_match = true;
|
||||
for (arg, alias_arg) in substs.iter().zip(alias_substs.iter()) {
|
||||
if let (Some(arg_ty), Some(alias_arg_ty)) = (arg.as_type(), alias_arg.as_type()) {
|
||||
if matches!(alias_arg_ty.kind(), ty::Param(_)) {
|
||||
continue;
|
||||
}
|
||||
if let (ty::Adt(arg_adt, _), ty::Adt(alias_arg_adt, _)) =
|
||||
(arg_ty.peel_refs().kind(), alias_arg_ty.peel_refs().kind())
|
||||
{
|
||||
if arg_adt.did() != alias_arg_adt.did() {
|
||||
all_match = false;
|
||||
break;
|
||||
}
|
||||
} else if arg_ty != alias_arg_ty {
|
||||
all_match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for &def_id in unwrap_allowed_aliases {
|
||||
let alias_ty = cx.tcx.type_of(def_id).instantiate_identity();
|
||||
if let (ty::Adt(adt, substs), ty::Adt(alias_adt, alias_substs)) = (ty.kind(), alias_ty.kind())
|
||||
&& adt.did() == alias_adt.did()
|
||||
{
|
||||
let mut all_match = true;
|
||||
for (arg, alias_arg) in substs.iter().zip(alias_substs.iter()) {
|
||||
if let (Some(arg_ty), Some(alias_arg_ty)) = (arg.as_type(), alias_arg.as_type()) {
|
||||
if matches!(alias_arg_ty.kind(), ty::Param(_)) {
|
||||
continue;
|
||||
}
|
||||
if all_match {
|
||||
return;
|
||||
if let (ty::Adt(arg_adt, _), ty::Adt(alias_arg_adt, _)) =
|
||||
(arg_ty.peel_refs().kind(), alias_arg_ty.peel_refs().kind())
|
||||
{
|
||||
if arg_adt.did() != alias_arg_adt.did() {
|
||||
all_match = false;
|
||||
break;
|
||||
}
|
||||
} else if arg_ty != alias_arg_ty {
|
||||
all_match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if all_match {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +138,8 @@ pub(super) fn check_call(
|
||||
allow_unwrap_in_tests: bool,
|
||||
allow_expect_in_consts: bool,
|
||||
allow_expect_in_tests: bool,
|
||||
allow_unwrap_types: &[String],
|
||||
unwrap_allowed_ids: &rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::DefId>,
|
||||
unwrap_allowed_aliases: &[rustc_hir::def_id::DefId],
|
||||
) {
|
||||
let Some(recv) = args.first() else {
|
||||
return;
|
||||
@@ -167,7 +157,8 @@ pub(super) fn check_call(
|
||||
false,
|
||||
allow_unwrap_in_consts,
|
||||
allow_unwrap_in_tests,
|
||||
allow_unwrap_types,
|
||||
unwrap_allowed_ids,
|
||||
unwrap_allowed_aliases,
|
||||
Variant::Unwrap,
|
||||
);
|
||||
},
|
||||
@@ -179,7 +170,8 @@ pub(super) fn check_call(
|
||||
false,
|
||||
allow_expect_in_consts,
|
||||
allow_expect_in_tests,
|
||||
allow_unwrap_types,
|
||||
unwrap_allowed_ids,
|
||||
unwrap_allowed_aliases,
|
||||
Variant::Expect,
|
||||
);
|
||||
},
|
||||
@@ -191,7 +183,8 @@ pub(super) fn check_call(
|
||||
true,
|
||||
allow_unwrap_in_consts,
|
||||
allow_unwrap_in_tests,
|
||||
allow_unwrap_types,
|
||||
unwrap_allowed_ids,
|
||||
unwrap_allowed_aliases,
|
||||
Variant::Unwrap,
|
||||
);
|
||||
},
|
||||
@@ -203,7 +196,8 @@ pub(super) fn check_call(
|
||||
true,
|
||||
allow_expect_in_consts,
|
||||
allow_expect_in_tests,
|
||||
allow_unwrap_types,
|
||||
unwrap_allowed_ids,
|
||||
unwrap_allowed_aliases,
|
||||
Variant::Expect,
|
||||
);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user