mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
Simplify clippy
fix.
This commit is contained in:
parent
c0e249ce67
commit
64a42db51a
@ -4,15 +4,15 @@ use clippy_utils::source::snippet;
|
|||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::ty::{is_type_diagnostic_item, needs_ordered_drop};
|
use clippy_utils::ty::{is_type_diagnostic_item, needs_ordered_drop};
|
||||||
use clippy_utils::visitors::any_temporaries_need_ordered_drop;
|
use clippy_utils::visitors::any_temporaries_need_ordered_drop;
|
||||||
use clippy_utils::{higher, is_lang_ctor, is_trait_method, match_def_path, paths};
|
use clippy_utils::{higher, is_lang_ctor, is_trait_method};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast::LitKind;
|
use rustc_ast::ast::LitKind;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::LangItem::{OptionNone, PollPending};
|
use rustc_hir::LangItem::{self, OptionSome, OptionNone, PollPending, PollReady, ResultOk, ResultErr};
|
||||||
use rustc_hir::{Arm, Expr, ExprKind, Node, Pat, PatKind, QPath, UnOp};
|
use rustc_hir::{Arm, Expr, ExprKind, Node, Pat, PatKind, QPath, UnOp};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_middle::ty::{self, subst::GenericArgKind, DefIdTree, Ty};
|
use rustc_middle::ty::{self, subst::GenericArgKind, DefIdTree, Ty};
|
||||||
use rustc_span::{sym, Symbol, def_id::DefId};
|
use rustc_span::{sym, Symbol};
|
||||||
|
|
||||||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||||
if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(expr) {
|
if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(expr) {
|
||||||
@ -75,9 +75,9 @@ fn find_sugg_for_if_let<'tcx>(
|
|||||||
("is_some()", op_ty)
|
("is_some()", op_ty)
|
||||||
} else if Some(id) == lang_items.poll_ready_variant() {
|
} else if Some(id) == lang_items.poll_ready_variant() {
|
||||||
("is_ready()", op_ty)
|
("is_ready()", op_ty)
|
||||||
} else if is_pat_variant(cx, check_pat, qpath, &paths::IPADDR_V4, Item::Diag(sym!(IpAddr), sym!(V4))) {
|
} else if is_pat_variant(cx, check_pat, qpath, Item::Diag(sym::IpAddr, sym!(V4))) {
|
||||||
("is_ipv4()", op_ty)
|
("is_ipv4()", op_ty)
|
||||||
} else if is_pat_variant(cx, check_pat, qpath, &paths::IPADDR_V6, Item::Diag(sym!(IpAddr), sym!(V6))) {
|
} else if is_pat_variant(cx, check_pat, qpath, Item::Diag(sym::IpAddr, sym!(V6))) {
|
||||||
("is_ipv6()", op_ty)
|
("is_ipv6()", op_ty)
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
@ -174,7 +174,6 @@ fn find_sugg_for_if_let<'tcx>(
|
|||||||
|
|
||||||
pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
|
pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
|
||||||
if arms.len() == 2 {
|
if arms.len() == 2 {
|
||||||
let lang_items = cx.tcx.lang_items();
|
|
||||||
let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
|
let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
|
||||||
|
|
||||||
let found_good_method = match node_pair {
|
let found_good_method = match node_pair {
|
||||||
@ -188,10 +187,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
|
|||||||
arms,
|
arms,
|
||||||
path_left,
|
path_left,
|
||||||
path_right,
|
path_right,
|
||||||
&paths::RESULT_OK,
|
Item::Lang(ResultOk),
|
||||||
Item::Lang(lang_items.result_ok_variant()),
|
Item::Lang(ResultErr),
|
||||||
&paths::RESULT_ERR,
|
|
||||||
Item::Lang(lang_items.result_err_variant()),
|
|
||||||
"is_ok()",
|
"is_ok()",
|
||||||
"is_err()",
|
"is_err()",
|
||||||
)
|
)
|
||||||
@ -201,10 +198,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
|
|||||||
arms,
|
arms,
|
||||||
path_left,
|
path_left,
|
||||||
path_right,
|
path_right,
|
||||||
&paths::IPADDR_V4,
|
Item::Diag(sym::IpAddr, sym!(V4)),
|
||||||
Item::Diag(sym!(IpAddr), sym!(V4)),
|
Item::Diag(sym::IpAddr, sym!(V6)),
|
||||||
&paths::IPADDR_V6,
|
|
||||||
Item::Diag(sym!(IpAddr), sym!(V6)),
|
|
||||||
"is_ipv4()",
|
"is_ipv4()",
|
||||||
"is_ipv6()",
|
"is_ipv6()",
|
||||||
)
|
)
|
||||||
@ -224,10 +219,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
|
|||||||
arms,
|
arms,
|
||||||
path_left,
|
path_left,
|
||||||
path_right,
|
path_right,
|
||||||
&paths::OPTION_SOME,
|
Item::Lang(OptionSome),
|
||||||
Item::Lang(lang_items.option_some_variant()),
|
Item::Lang(OptionNone),
|
||||||
&paths::OPTION_NONE,
|
|
||||||
Item::Lang(lang_items.option_none_variant()),
|
|
||||||
"is_some()",
|
"is_some()",
|
||||||
"is_none()",
|
"is_none()",
|
||||||
)
|
)
|
||||||
@ -237,10 +230,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
|
|||||||
arms,
|
arms,
|
||||||
path_left,
|
path_left,
|
||||||
path_right,
|
path_right,
|
||||||
&paths::POLL_READY,
|
Item::Lang(PollReady),
|
||||||
Item::Lang(lang_items.poll_ready_variant()),
|
Item::Lang(PollPending),
|
||||||
&paths::POLL_PENDING,
|
|
||||||
Item::Lang(lang_items.poll_pending_variant()),
|
|
||||||
"is_ready()",
|
"is_ready()",
|
||||||
"is_pending()",
|
"is_pending()",
|
||||||
)
|
)
|
||||||
@ -278,21 +269,17 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
|
|||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum Item {
|
enum Item {
|
||||||
Lang(Option<DefId>),
|
Lang(LangItem),
|
||||||
Diag(Symbol, Symbol),
|
Diag(Symbol, Symbol),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_pat_variant(cx: &LateContext<'_>, pat: &Pat<'_>, path: &QPath<'_>, expected_path: &[&str], expected_item: Item) -> bool {
|
fn is_pat_variant(cx: &LateContext<'_>, pat: &Pat<'_>, path: &QPath<'_>, expected_item: Item) -> bool {
|
||||||
let Some(id) = cx.typeck_results().qpath_res(path, pat.hir_id).opt_def_id() else { return false };
|
let Some(id) = cx.typeck_results().qpath_res(path, pat.hir_id).opt_def_id() else { return false };
|
||||||
|
|
||||||
// TODO: Path matching can be removed when `IpAddr` is a diagnostic item.
|
|
||||||
if match_def_path(cx, id, expected_path) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
match expected_item {
|
match expected_item {
|
||||||
Item::Lang(expected_id) => {
|
Item::Lang(expected_lang_item) => {
|
||||||
Some(cx.tcx.parent(id)) == expected_id
|
let expected_id = cx.tcx.lang_items().require(expected_lang_item).unwrap();
|
||||||
|
cx.tcx.parent(id) == expected_id
|
||||||
},
|
},
|
||||||
Item::Diag(expected_ty, expected_variant) => {
|
Item::Diag(expected_ty, expected_variant) => {
|
||||||
let ty = cx.typeck_results().pat_ty(pat);
|
let ty = cx.typeck_results().pat_ty(pat);
|
||||||
@ -316,9 +303,7 @@ fn find_good_method_for_match<'a>(
|
|||||||
arms: &[Arm<'_>],
|
arms: &[Arm<'_>],
|
||||||
path_left: &QPath<'_>,
|
path_left: &QPath<'_>,
|
||||||
path_right: &QPath<'_>,
|
path_right: &QPath<'_>,
|
||||||
expected_path_left: &[&str],
|
|
||||||
expected_item_left: Item,
|
expected_item_left: Item,
|
||||||
expected_path_right: &[&str],
|
|
||||||
expected_item_right: Item,
|
expected_item_right: Item,
|
||||||
should_be_left: &'a str,
|
should_be_left: &'a str,
|
||||||
should_be_right: &'a str,
|
should_be_right: &'a str,
|
||||||
@ -327,15 +312,15 @@ fn find_good_method_for_match<'a>(
|
|||||||
let pat_right = arms[1].pat;
|
let pat_right = arms[1].pat;
|
||||||
|
|
||||||
let body_node_pair = if (
|
let body_node_pair = if (
|
||||||
is_pat_variant(cx, pat_left, path_left, expected_path_left, expected_item_left)
|
is_pat_variant(cx, pat_left, path_left, expected_item_left)
|
||||||
) && (
|
) && (
|
||||||
is_pat_variant(cx, pat_right, path_right, expected_path_right, expected_item_right)
|
is_pat_variant(cx, pat_right, path_right, expected_item_right)
|
||||||
) {
|
) {
|
||||||
(&arms[0].body.kind, &arms[1].body.kind)
|
(&arms[0].body.kind, &arms[1].body.kind)
|
||||||
} else if (
|
} else if (
|
||||||
is_pat_variant(cx, pat_left, path_left, expected_path_right, expected_item_right)
|
is_pat_variant(cx, pat_left, path_left, expected_item_right)
|
||||||
) && (
|
) && (
|
||||||
is_pat_variant(cx, pat_right, path_right, expected_path_left, expected_item_left)
|
is_pat_variant(cx, pat_right, path_right, expected_item_left)
|
||||||
) {
|
) {
|
||||||
(&arms[1].body.kind, &arms[0].body.kind)
|
(&arms[1].body.kind, &arms[0].body.kind)
|
||||||
} else {
|
} else {
|
||||||
|
@ -66,8 +66,6 @@ pub const INDEX_MUT: [&str; 3] = ["core", "ops", "IndexMut"];
|
|||||||
pub const INSERT_STR: [&str; 4] = ["alloc", "string", "String", "insert_str"];
|
pub const INSERT_STR: [&str; 4] = ["alloc", "string", "String", "insert_str"];
|
||||||
pub const IO_READ: [&str; 3] = ["std", "io", "Read"];
|
pub const IO_READ: [&str; 3] = ["std", "io", "Read"];
|
||||||
pub const IO_WRITE: [&str; 3] = ["std", "io", "Write"];
|
pub const IO_WRITE: [&str; 3] = ["std", "io", "Write"];
|
||||||
pub const IPADDR_V4: [&str; 5] = ["std", "net", "ip", "IpAddr", "V4"];
|
|
||||||
pub const IPADDR_V6: [&str; 5] = ["std", "net", "ip", "IpAddr", "V6"];
|
|
||||||
pub const ITER_COUNT: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "count"];
|
pub const ITER_COUNT: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "count"];
|
||||||
pub const ITER_EMPTY: [&str; 5] = ["core", "iter", "sources", "empty", "Empty"];
|
pub const ITER_EMPTY: [&str; 5] = ["core", "iter", "sources", "empty", "Empty"];
|
||||||
pub const ITER_REPEAT: [&str; 5] = ["core", "iter", "sources", "repeat", "repeat"];
|
pub const ITER_REPEAT: [&str; 5] = ["core", "iter", "sources", "repeat", "repeat"];
|
||||||
|
Loading…
Reference in New Issue
Block a user