mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 15:14:29 +00:00
Auto merge of #83188 - petrochenkov:field, r=lcnr
ast/hir: Rename field-related structures I always forget what `ast::Field` and `ast::StructField` mean despite working with AST for long time, so this PR changes the naming to less confusing and more consistent. - `StructField` -> `FieldDef` ("field definition") - `Field` -> `ExprField` ("expression field", not "field expression") - `FieldPat` -> `PatField` ("pattern field", not "field pattern") Various visiting and other methods working with the fields are renamed correspondingly too. The second commit reduces the size of `ExprKind` by boxing fields of `ExprKind::Struct` in preparation for https://github.com/rust-lang/rust/pull/80080.
This commit is contained in:
commit
56138ad789
13 changed files with 29 additions and 27 deletions
|
@ -119,7 +119,7 @@ impl LateLintPass<'_> for InconsistentStructConstructor {
|
|||
|
||||
// Check whether the order of the fields in the constructor is consistent with the order in the
|
||||
// definition.
|
||||
fn is_consistent_order<'tcx>(fields: &'tcx [hir::Field<'tcx>], def_order_map: &FxHashMap<Symbol, usize>) -> bool {
|
||||
fn is_consistent_order<'tcx>(fields: &'tcx [hir::ExprField<'tcx>], def_order_map: &FxHashMap<Symbol, usize>) -> bool {
|
||||
let mut cur_idx = usize::MIN;
|
||||
for f in fields {
|
||||
let next_idx = def_order_map[&f.ident.name];
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::utils::{meets_msrv, snippet_opt, span_lint_and_then};
|
||||
use if_chain::if_chain;
|
||||
use rustc_ast::ast::{Attribute, Item, ItemKind, StructField, Variant, VariantData, VisibilityKind};
|
||||
use rustc_ast::ast::{Attribute, Item, ItemKind, FieldDef, Variant, VariantData, VisibilityKind};
|
||||
use rustc_attr as attr;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||
|
@ -142,11 +142,11 @@ fn check_manual_non_exhaustive_enum(cx: &EarlyContext<'_>, item: &Item, variants
|
|||
}
|
||||
|
||||
fn check_manual_non_exhaustive_struct(cx: &EarlyContext<'_>, item: &Item, data: &VariantData) {
|
||||
fn is_private(field: &StructField) -> bool {
|
||||
fn is_private(field: &FieldDef) -> bool {
|
||||
matches!(field.vis.kind, VisibilityKind::Inherited)
|
||||
}
|
||||
|
||||
fn is_non_exhaustive_marker(field: &StructField) -> bool {
|
||||
fn is_non_exhaustive_marker(field: &FieldDef) -> bool {
|
||||
is_private(field) && field.ty.kind.is_unit() && field.ident.map_or(true, |n| n.as_str().starts_with('_'))
|
||||
}
|
||||
|
||||
|
|
|
@ -188,7 +188,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
|
|||
self.check_missing_docs_attrs(cx, attrs, impl_item.span, article, desc);
|
||||
}
|
||||
|
||||
fn check_struct_field(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::StructField<'_>) {
|
||||
fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_>) {
|
||||
if !sf.is_positional() {
|
||||
let attrs = cx.tcx.hir().attrs(sf.hir_id);
|
||||
self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::utils::{last_path_segment, span_lint_and_help};
|
||||
use rustc_hir::{
|
||||
intravisit, Body, Expr, ExprKind, FieldPat, FnDecl, HirId, LocalSource, MatchSource, Mutability, Pat, PatKind,
|
||||
intravisit, Body, Expr, ExprKind, PatField, FnDecl, HirId, LocalSource, MatchSource, Mutability, Pat, PatKind,
|
||||
QPath, Stmt, StmtKind,
|
||||
};
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
|
@ -281,7 +281,7 @@ where
|
|||
|
||||
fn find_first_mismatch_in_struct<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
field_pats: &[FieldPat<'_>],
|
||||
field_pats: &[PatField<'_>],
|
||||
field_defs: &[FieldDef],
|
||||
substs_ref: SubstsRef<'tcx>,
|
||||
) -> Option<(Span, Mutability, Level)> {
|
||||
|
|
|
@ -58,8 +58,8 @@ impl EarlyLintPass for RedundantFieldNames {
|
|||
if in_external_macro(cx.sess, expr.span) {
|
||||
return;
|
||||
}
|
||||
if let ExprKind::Struct(_, ref fields, _) = expr.kind {
|
||||
for field in fields {
|
||||
if let ExprKind::Struct(ref se) = expr.kind {
|
||||
for field in &se.fields {
|
||||
if field.is_shorthand {
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -564,7 +564,7 @@ fn ident_difference_expr_with_base_location(
|
|||
| (Try(_), Try(_))
|
||||
| (Paren(_), Paren(_))
|
||||
| (Repeat(_, _), Repeat(_, _))
|
||||
| (Struct(_, _, _), Struct(_, _, _))
|
||||
| (Struct(_), Struct(_))
|
||||
| (MacCall(_), MacCall(_))
|
||||
| (LlvmInlineAsm(_), LlvmInlineAsm(_))
|
||||
| (InlineAsm(_), InlineAsm(_))
|
||||
|
|
|
@ -271,7 +271,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
|
|||
self.check_fn_decl(cx, decl);
|
||||
}
|
||||
|
||||
fn check_struct_field(&mut self, cx: &LateContext<'_>, field: &hir::StructField<'_>) {
|
||||
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
|
||||
self.check_ty(cx, &field.ty, false);
|
||||
}
|
||||
|
||||
|
@ -821,7 +821,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeComplexity {
|
|||
self.check_fndecl(cx, decl);
|
||||
}
|
||||
|
||||
fn check_struct_field(&mut self, cx: &LateContext<'tcx>, field: &'tcx hir::StructField<'_>) {
|
||||
fn check_field_def(&mut self, cx: &LateContext<'tcx>, field: &'tcx hir::FieldDef<'_>) {
|
||||
// enum variants are also struct fields now
|
||||
self.check_type(cx, &field.ty);
|
||||
}
|
||||
|
|
|
@ -276,7 +276,7 @@ fn transform_with_focus_on_idx(alternatives: &mut Vec<P<Pat>>, focus_idx: usize)
|
|||
/// and check that all `fp_i` where `i ∈ ((0...n) \ k)` between two patterns are equal.
|
||||
fn extend_with_struct_pat(
|
||||
path1: &ast::Path,
|
||||
fps1: &mut Vec<ast::FieldPat>,
|
||||
fps1: &mut Vec<ast::PatField>,
|
||||
rest1: bool,
|
||||
start: usize,
|
||||
alternatives: &mut Vec<P<Pat>>,
|
||||
|
|
|
@ -101,12 +101,12 @@ impl<'tcx> LateLintPass<'tcx> for Author {
|
|||
done();
|
||||
}
|
||||
|
||||
fn check_struct_field(&mut self, cx: &LateContext<'tcx>, field: &'tcx hir::StructField<'_>) {
|
||||
fn check_field_def(&mut self, cx: &LateContext<'tcx>, field: &'tcx hir::FieldDef<'_>) {
|
||||
if !has_attr(cx, field.hir_id) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
PrintVisitor::new("field").visit_struct_field(field);
|
||||
PrintVisitor::new("field").visit_field_def(field);
|
||||
done();
|
||||
}
|
||||
|
||||
|
|
|
@ -80,8 +80,8 @@ impl<'tcx> LateLintPass<'tcx> for DeepCodeInspector {
|
|||
// }
|
||||
// }
|
||||
//
|
||||
// fn check_struct_field(&mut self, cx: &LateContext<'tcx>, field: &'tcx
|
||||
// hir::StructField) {
|
||||
// fn check_field_def(&mut self, cx: &LateContext<'tcx>, field: &'tcx
|
||||
// hir::FieldDef) {
|
||||
// if !has_attr(&field.attrs) {
|
||||
// return;
|
||||
// }
|
||||
|
|
|
@ -66,7 +66,7 @@ pub fn eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn eq_field_pat(l: &FieldPat, r: &FieldPat) -> bool {
|
||||
pub fn eq_field_pat(l: &PatField, r: &PatField) -> bool {
|
||||
l.is_placeholder == r.is_placeholder
|
||||
&& eq_id(l.ident, r.ident)
|
||||
&& eq_pat(&l.pat, &r.pat)
|
||||
|
@ -168,14 +168,16 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
|
|||
(AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
|
||||
(Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
|
||||
(MacCall(l), MacCall(r)) => eq_mac_call(l, r),
|
||||
(Struct(lp, lfs, lb), Struct(rp, rfs, rb)) => {
|
||||
eq_path(lp, rp) && eq_struct_rest(lb, rb) && unordered_over(lfs, rfs, |l, r| eq_field(l, r))
|
||||
(Struct(lse), Struct(rse)) => {
|
||||
eq_path(&lse.path, &rse.path) &&
|
||||
eq_struct_rest(&lse.rest, &rse.rest) &&
|
||||
unordered_over(&lse.fields, &rse.fields, |l, r| eq_field(l, r))
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eq_field(l: &Field, r: &Field) -> bool {
|
||||
pub fn eq_field(l: &ExprField, r: &ExprField) -> bool {
|
||||
l.is_placeholder == r.is_placeholder
|
||||
&& eq_id(l.ident, r.ident)
|
||||
&& eq_expr(&l.expr, &r.expr)
|
||||
|
@ -359,7 +361,7 @@ pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn eq_struct_field(l: &StructField, r: &StructField) -> bool {
|
||||
pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
|
||||
l.is_placeholder == r.is_placeholder
|
||||
&& over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
|
||||
&& eq_vis(&l.vis, &r.vis)
|
||||
|
|
|
@ -51,7 +51,7 @@ pub struct Range<'a> {
|
|||
pub fn range<'a>(expr: &'a hir::Expr<'_>) -> Option<Range<'a>> {
|
||||
/// Finds the field named `name` in the field. Always return `Some` for
|
||||
/// convenience.
|
||||
fn get_field<'c>(name: &str, fields: &'c [hir::Field<'_>]) -> Option<&'c hir::Expr<'c>> {
|
||||
fn get_field<'c>(name: &str, fields: &'c [hir::ExprField<'_>]) -> Option<&'c hir::Expr<'c>> {
|
||||
let expr = &fields.iter().find(|field| field.ident.name.as_str() == name)?.expr;
|
||||
|
||||
Some(expr)
|
||||
|
|
|
@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHashMap;
|
|||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||
use rustc_hir::def::Res;
|
||||
use rustc_hir::{
|
||||
BinOpKind, Block, BlockCheckMode, BodyId, BorrowKind, CaptureBy, Expr, ExprKind, Field, FieldPat, FnRetTy,
|
||||
BinOpKind, Block, BlockCheckMode, BodyId, BorrowKind, CaptureBy, Expr, ExprKind, ExprField, PatField, FnRetTy,
|
||||
GenericArg, GenericArgs, Guard, HirId, InlineAsmOperand, Lifetime, LifetimeName, ParamName, Pat, PatKind, Path,
|
||||
PathSegment, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding,
|
||||
};
|
||||
|
@ -266,7 +266,7 @@ impl HirEqInterExpr<'_, '_, '_> {
|
|||
over(left, right, |l, r| self.eq_expr(l, r))
|
||||
}
|
||||
|
||||
fn eq_field(&mut self, left: &Field<'_>, right: &Field<'_>) -> bool {
|
||||
fn eq_field(&mut self, left: &ExprField<'_>, right: &ExprField<'_>) -> bool {
|
||||
left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
|
||||
}
|
||||
|
||||
|
@ -290,8 +290,8 @@ impl HirEqInterExpr<'_, '_, '_> {
|
|||
left.name == right.name
|
||||
}
|
||||
|
||||
fn eq_fieldpat(&mut self, left: &FieldPat<'_>, right: &FieldPat<'_>) -> bool {
|
||||
let (FieldPat { ident: li, pat: lp, .. }, FieldPat { ident: ri, pat: rp, .. }) = (&left, &right);
|
||||
fn eq_fieldpat(&mut self, left: &PatField<'_>, right: &PatField<'_>) -> bool {
|
||||
let (PatField { ident: li, pat: lp, .. }, PatField { ident: ri, pat: rp, .. }) = (&left, &right);
|
||||
li.name == ri.name && self.eq_pat(lp, rp)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue