Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/tools/clippy/clippy_lints/src/matches/match_same_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_hir::def_id::DefId;
use rustc_hir::{Arm, Expr, HirId, HirIdMap, HirIdMapEntry, HirIdSet, Pat, PatExpr, PatExprKind, PatKind, RangeEnd};
use rustc_lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
use rustc_lint::{LateContext, LintContext};
use rustc_middle::ty;
use rustc_middle::ty::{self, TypeckResults};
use rustc_span::{ByteSymbol, ErrorGuaranteed, Span, Symbol};

use super::MATCH_SAME_ARMS;
Expand Down Expand Up @@ -61,7 +61,10 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {

let check_eq_with_pat = |expr_a: &Expr<'_>, expr_b: &Expr<'_>| {
let mut local_map: HirIdMap<HirId> = HirIdMap::default();
let eq_fallback = |a: &Expr<'_>, b: &Expr<'_>| {
let eq_fallback = |a_typeck_results: &TypeckResults<'tcx>,
a: &Expr<'_>,
b_typeck_results: &TypeckResults<'tcx>,
b: &Expr<'_>| {
if let Some(a_id) = a.res_local_id()
&& let Some(b_id) = b.res_local_id()
&& let entry = match local_map.entry(a_id) {
Expand All @@ -71,7 +74,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
}
// the names technically don't have to match; this makes the lint more conservative
&& cx.tcx.hir_name(a_id) == cx.tcx.hir_name(b_id)
&& cx.typeck_results().expr_ty(a) == cx.typeck_results().expr_ty(b)
&& a_typeck_results.expr_ty(a) == b_typeck_results.expr_ty(b)
&& pat_contains_local(lhs.pat, a_id)
&& pat_contains_local(rhs.pat, b_id)
{
Expand Down
7 changes: 5 additions & 2 deletions src/tools/clippy/clippy_lints/src/methods/filter_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::{Closure, Expr, ExprKind, PatKind, PathSegment, QPath, UnOp};
use rustc_lint::LateContext;
use rustc_middle::ty::TypeckResults;
use rustc_middle::ty::adjustment::Adjust;
use rustc_span::Span;
use rustc_span::symbol::{Ident, Symbol};
Expand Down Expand Up @@ -136,15 +137,17 @@ impl<'tcx> OffendingFilterExpr<'tcx> {
// .map(|y| y[.acceptable_method()].unwrap())
&& let simple_equal = (receiver.res_local_id() == Some(filter_param_id)
&& map_arg_peeled.res_local_id() == Some(map_param_id))
&& let eq_fallback = (|a: &Expr<'_>, b: &Expr<'_>| {
&& let eq_fallback =
(|a_typeck_results: &TypeckResults<'tcx>, a: &Expr<'_>,
b_typeck_results: &TypeckResults<'tcx>, b: &Expr<'_>| {
// in `filter(|x| ..)`, replace `*x` with `x`
let a_path = if !is_filter_param_ref
&& let ExprKind::Unary(UnOp::Deref, expr_path) = a.kind
{ expr_path } else { a };
// let the filter closure arg and the map closure arg be equal
a_path.res_local_id() == Some(filter_param_id)
&& b.res_local_id() == Some(map_param_id)
&& cx.typeck_results().expr_ty_adjusted(a) == cx.typeck_results().expr_ty_adjusted(b)
&& a_typeck_results.expr_ty_adjusted(a) == b_typeck_results.expr_ty_adjusted(b)
})
&& (simple_equal
|| SpanlessEq::new(cx).expr_fallback(eq_fallback).eq_expr(receiver, map_arg_peeled))
Expand Down
32 changes: 27 additions & 5 deletions src/tools/clippy/clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -4778,6 +4780,8 @@ impl Methods {
allowed_dotfiles,
format_args,
allow_unwrap_types: conf.allow_unwrap_types.clone(),
unwrap_allowed_ids: FxHashSet::default(),
unwrap_allowed_aliases: Vec::new(),
}
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -4976,7 +4993,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
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(..) => {
Expand Down Expand Up @@ -5730,7 +5748,8 @@ impl Methods {
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);
Expand All @@ -5743,7 +5762,8 @@ impl Methods {
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,
);
},
Expand All @@ -5764,7 +5784,8 @@ impl Methods {
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,
);
},
Expand All @@ -5776,7 +5797,8 @@ impl Methods {
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,
);
},
Expand Down
80 changes: 37 additions & 43 deletions src/tools/clippy/clippy_lints/src/methods/unwrap_expect_used.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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,
);
},
Expand All @@ -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,
);
},
Expand All @@ -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,
);
},
Expand All @@ -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,
);
},
Expand Down
Loading
Loading