mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-30 18:53:39 +00:00
Address unused tuple struct fields in clippy
This commit is contained in:
parent
a4c2cf181b
commit
de06ce886e
@ -22,7 +22,7 @@ pub(super) enum Op<'a> {
|
|||||||
|
|
||||||
// rm `.cloned()`
|
// rm `.cloned()`
|
||||||
// e.g. `map` `for_each` `all` `any`
|
// e.g. `map` `for_each` `all` `any`
|
||||||
NeedlessMove(&'a str, &'a Expr<'a>),
|
NeedlessMove(&'a Expr<'a>),
|
||||||
|
|
||||||
// later `.cloned()`
|
// later `.cloned()`
|
||||||
// and add `&` to the parameter of closure parameter
|
// and add `&` to the parameter of closure parameter
|
||||||
@ -59,7 +59,7 @@ pub(super) fn check<'tcx>(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Op::NeedlessMove(_, expr) = op {
|
if let Op::NeedlessMove(expr) = op {
|
||||||
let rustc_hir::ExprKind::Closure(closure) = expr.kind else {
|
let rustc_hir::ExprKind::Closure(closure) = expr.kind else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@ -104,7 +104,7 @@ pub(super) fn check<'tcx>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let (lint, msg, trailing_clone) = match op {
|
let (lint, msg, trailing_clone) = match op {
|
||||||
Op::RmCloned | Op::NeedlessMove(_, _) => (REDUNDANT_CLONE, "unneeded cloning of iterator items", ""),
|
Op::RmCloned | Op::NeedlessMove(_) => (REDUNDANT_CLONE, "unneeded cloning of iterator items", ""),
|
||||||
Op::LaterCloned | Op::FixClosure(_, _) => (
|
Op::LaterCloned | Op::FixClosure(_, _) => (
|
||||||
ITER_OVEREAGER_CLONED,
|
ITER_OVEREAGER_CLONED,
|
||||||
"unnecessarily eager cloning of iterator items",
|
"unnecessarily eager cloning of iterator items",
|
||||||
@ -133,7 +133,7 @@ pub(super) fn check<'tcx>(
|
|||||||
diag.span_suggestion(replace_span, "try", snip, Applicability::MachineApplicable);
|
diag.span_suggestion(replace_span, "try", snip, Applicability::MachineApplicable);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Op::NeedlessMove(_, _) => {
|
Op::NeedlessMove(_) => {
|
||||||
let method_span = expr.span.with_lo(cloned_call.span.hi());
|
let method_span = expr.span.with_lo(cloned_call.span.hi());
|
||||||
if let Some(snip) = snippet_opt(cx, method_span) {
|
if let Some(snip) = snippet_opt(cx, method_span) {
|
||||||
let replace_span = expr.span.with_lo(cloned_recv.span.hi());
|
let replace_span = expr.span.with_lo(cloned_recv.span.hi());
|
||||||
|
@ -4186,7 +4186,7 @@ impl Methods {
|
|||||||
expr,
|
expr,
|
||||||
recv,
|
recv,
|
||||||
recv2,
|
recv2,
|
||||||
iter_overeager_cloned::Op::NeedlessMove(name, arg),
|
iter_overeager_cloned::Op::NeedlessMove(arg),
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -4204,7 +4204,7 @@ impl Methods {
|
|||||||
expr,
|
expr,
|
||||||
recv,
|
recv,
|
||||||
recv2,
|
recv2,
|
||||||
iter_overeager_cloned::Op::NeedlessMove(name, arg),
|
iter_overeager_cloned::Op::NeedlessMove(arg),
|
||||||
false,
|
false,
|
||||||
),
|
),
|
||||||
Some(("chars", recv, _, _, _))
|
Some(("chars", recv, _, _, _))
|
||||||
@ -4379,7 +4379,7 @@ impl Methods {
|
|||||||
expr,
|
expr,
|
||||||
recv,
|
recv,
|
||||||
recv2,
|
recv2,
|
||||||
iter_overeager_cloned::Op::NeedlessMove(name, arg),
|
iter_overeager_cloned::Op::NeedlessMove(arg),
|
||||||
false,
|
false,
|
||||||
),
|
),
|
||||||
_ => {},
|
_ => {},
|
||||||
@ -4433,7 +4433,7 @@ impl Methods {
|
|||||||
expr,
|
expr,
|
||||||
recv,
|
recv,
|
||||||
recv2,
|
recv2,
|
||||||
iter_overeager_cloned::Op::NeedlessMove(name, m_arg),
|
iter_overeager_cloned::Op::NeedlessMove(m_arg),
|
||||||
false,
|
false,
|
||||||
),
|
),
|
||||||
_ => {},
|
_ => {},
|
||||||
|
@ -80,7 +80,6 @@ enum IfBlockType<'hir> {
|
|||||||
Ty<'hir>,
|
Ty<'hir>,
|
||||||
Symbol,
|
Symbol,
|
||||||
&'hir Expr<'hir>,
|
&'hir Expr<'hir>,
|
||||||
Option<&'hir Expr<'hir>>,
|
|
||||||
),
|
),
|
||||||
/// An `if let Xxx(a) = b { c } else { d }` expression.
|
/// An `if let Xxx(a) = b { c } else { d }` expression.
|
||||||
///
|
///
|
||||||
@ -143,7 +142,7 @@ fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
|
|||||||
|
|
||||||
fn is_early_return(smbl: Symbol, cx: &LateContext<'_>, if_block: &IfBlockType<'_>) -> bool {
|
fn is_early_return(smbl: Symbol, cx: &LateContext<'_>, if_block: &IfBlockType<'_>) -> bool {
|
||||||
match *if_block {
|
match *if_block {
|
||||||
IfBlockType::IfIs(caller, caller_ty, call_sym, if_then, _) => {
|
IfBlockType::IfIs(caller, caller_ty, call_sym, if_then) => {
|
||||||
// If the block could be identified as `if x.is_none()/is_err()`,
|
// If the block could be identified as `if x.is_none()/is_err()`,
|
||||||
// we then only need to check the if_then return to see if it is none/err.
|
// we then only need to check the if_then return to see if it is none/err.
|
||||||
is_type_diagnostic_item(cx, caller_ty, smbl)
|
is_type_diagnostic_item(cx, caller_ty, smbl)
|
||||||
@ -235,7 +234,7 @@ impl QuestionMark {
|
|||||||
&& !is_else_clause(cx.tcx, expr)
|
&& !is_else_clause(cx.tcx, expr)
|
||||||
&& let ExprKind::MethodCall(segment, caller, ..) = &cond.kind
|
&& let ExprKind::MethodCall(segment, caller, ..) = &cond.kind
|
||||||
&& let caller_ty = cx.typeck_results().expr_ty(caller)
|
&& let caller_ty = cx.typeck_results().expr_ty(caller)
|
||||||
&& let if_block = IfBlockType::IfIs(caller, caller_ty, segment.ident.name, then, r#else)
|
&& let if_block = IfBlockType::IfIs(caller, caller_ty, segment.ident.name, then)
|
||||||
&& (is_early_return(sym::Option, cx, &if_block) || is_early_return(sym::Result, cx, &if_block))
|
&& (is_early_return(sym::Option, cx, &if_block) || is_early_return(sym::Result, cx, &if_block))
|
||||||
{
|
{
|
||||||
let mut applicability = Applicability::MachineApplicable;
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
|
@ -26,18 +26,18 @@ pub(super) fn check<'tcx>(
|
|||||||
|
|
||||||
// `Repr(C)` <-> unordered type.
|
// `Repr(C)` <-> unordered type.
|
||||||
// If the first field of the `Repr(C)` type matches then the transmute is ok
|
// If the first field of the `Repr(C)` type matches then the transmute is ok
|
||||||
(ReducedTy::OrderedFields(_, Some(from_sub_ty)), ReducedTy::UnorderedFields(to_sub_ty))
|
(ReducedTy::OrderedFields(Some(from_sub_ty)), ReducedTy::UnorderedFields(to_sub_ty))
|
||||||
| (ReducedTy::UnorderedFields(from_sub_ty), ReducedTy::OrderedFields(_, Some(to_sub_ty))) => {
|
| (ReducedTy::UnorderedFields(from_sub_ty), ReducedTy::OrderedFields(Some(to_sub_ty))) => {
|
||||||
from_ty = from_sub_ty;
|
from_ty = from_sub_ty;
|
||||||
to_ty = to_sub_ty;
|
to_ty = to_sub_ty;
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
(ReducedTy::OrderedFields(_, Some(from_sub_ty)), ReducedTy::Other(to_sub_ty)) if reduced_tys.to_fat_ptr => {
|
(ReducedTy::OrderedFields(Some(from_sub_ty)), ReducedTy::Other(to_sub_ty)) if reduced_tys.to_fat_ptr => {
|
||||||
from_ty = from_sub_ty;
|
from_ty = from_sub_ty;
|
||||||
to_ty = to_sub_ty;
|
to_ty = to_sub_ty;
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
(ReducedTy::Other(from_sub_ty), ReducedTy::OrderedFields(_, Some(to_sub_ty)))
|
(ReducedTy::Other(from_sub_ty), ReducedTy::OrderedFields(Some(to_sub_ty)))
|
||||||
if reduced_tys.from_fat_ptr =>
|
if reduced_tys.from_fat_ptr =>
|
||||||
{
|
{
|
||||||
from_ty = from_sub_ty;
|
from_ty = from_sub_ty;
|
||||||
@ -235,8 +235,8 @@ enum ReducedTy<'tcx> {
|
|||||||
TypeErasure { raw_ptr_only: bool },
|
TypeErasure { raw_ptr_only: bool },
|
||||||
/// The type is a struct containing either zero non-zero sized fields, or multiple non-zero
|
/// The type is a struct containing either zero non-zero sized fields, or multiple non-zero
|
||||||
/// sized fields with a defined order.
|
/// sized fields with a defined order.
|
||||||
/// The second value is the first non-zero sized type.
|
/// The value is the first non-zero sized type.
|
||||||
OrderedFields(Ty<'tcx>, Option<Ty<'tcx>>),
|
OrderedFields(Option<Ty<'tcx>>),
|
||||||
/// The type is a struct containing multiple non-zero sized fields with no defined order.
|
/// The type is a struct containing multiple non-zero sized fields with no defined order.
|
||||||
UnorderedFields(Ty<'tcx>),
|
UnorderedFields(Ty<'tcx>),
|
||||||
/// Any other type.
|
/// Any other type.
|
||||||
@ -259,7 +259,7 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx>
|
|||||||
ty::Tuple(args) => {
|
ty::Tuple(args) => {
|
||||||
let mut iter = args.iter();
|
let mut iter = args.iter();
|
||||||
let Some(sized_ty) = iter.find(|&ty| !is_zero_sized_ty(cx, ty)) else {
|
let Some(sized_ty) = iter.find(|&ty| !is_zero_sized_ty(cx, ty)) else {
|
||||||
return ReducedTy::OrderedFields(ty, None);
|
return ReducedTy::OrderedFields(None);
|
||||||
};
|
};
|
||||||
if iter.all(|ty| is_zero_sized_ty(cx, ty)) {
|
if iter.all(|ty| is_zero_sized_ty(cx, ty)) {
|
||||||
ty = sized_ty;
|
ty = sized_ty;
|
||||||
@ -281,7 +281,7 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx>
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if def.repr().inhibit_struct_field_reordering_opt() {
|
if def.repr().inhibit_struct_field_reordering_opt() {
|
||||||
ReducedTy::OrderedFields(ty, Some(sized_ty))
|
ReducedTy::OrderedFields(Some(sized_ty))
|
||||||
} else {
|
} else {
|
||||||
ReducedTy::UnorderedFields(ty)
|
ReducedTy::UnorderedFields(ty)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user