2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet;
|
|
|
|
use if_chain::if_chain;
|
2021-02-25 04:25:22 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{self as hir, ExprKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
use rustc_span::symbol::Symbol;
|
2022-05-05 09:12:52 -05:00
|
|
|
use std::fmt::Write as _;
|
2021-02-25 04:25:22 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for struct constructors where all fields are shorthand and
|
2021-03-25 13:29:11 -05:00
|
|
|
/// the order of the field init shorthand in the constructor is inconsistent
|
|
|
|
/// with the order in the struct definition.
|
2021-02-25 04:25:22 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Since the order of fields in a constructor doesn't affect the
|
2021-02-25 04:25:22 -06:00
|
|
|
/// resulted instance as the below example indicates,
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// #[derive(Debug, PartialEq, Eq)]
|
|
|
|
/// struct Foo {
|
|
|
|
/// x: i32,
|
|
|
|
/// y: i32,
|
|
|
|
/// }
|
|
|
|
/// let x = 1;
|
|
|
|
/// let y = 2;
|
|
|
|
///
|
2021-03-25 13:29:11 -05:00
|
|
|
/// // This assertion never fails:
|
2021-02-25 04:25:22 -06:00
|
|
|
/// assert_eq!(Foo { x, y }, Foo { y, x });
|
|
|
|
/// ```
|
|
|
|
///
|
2021-03-25 13:29:11 -05:00
|
|
|
/// inconsistent order can be confusing and decreases readability and consistency.
|
2021-02-25 04:25:22 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2021-02-25 04:25:22 -06:00
|
|
|
/// ```rust
|
|
|
|
/// struct Foo {
|
|
|
|
/// x: i32,
|
|
|
|
/// y: i32,
|
|
|
|
/// }
|
|
|
|
/// let x = 1;
|
|
|
|
/// let y = 2;
|
2021-03-25 13:29:11 -05:00
|
|
|
///
|
2021-02-25 04:25:22 -06:00
|
|
|
/// Foo { y, x };
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// # struct Foo {
|
|
|
|
/// # x: i32,
|
|
|
|
/// # y: i32,
|
|
|
|
/// # }
|
|
|
|
/// # let x = 1;
|
|
|
|
/// # let y = 2;
|
|
|
|
/// Foo { x, y };
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.52.0"]
|
2021-02-25 04:25:22 -06:00
|
|
|
pub INCONSISTENT_STRUCT_CONSTRUCTOR,
|
2021-05-20 05:30:31 -05:00
|
|
|
pedantic,
|
2021-02-25 04:25:22 -06:00
|
|
|
"the order of the field init shorthand is inconsistent with the order in the struct definition"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(InconsistentStructConstructor => [INCONSISTENT_STRUCT_CONSTRUCTOR]);
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for InconsistentStructConstructor {
|
2021-02-25 04:25:22 -06:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
|
|
|
if_chain! {
|
2021-12-06 05:33:31 -06:00
|
|
|
if !expr.span.from_expansion();
|
2021-02-25 04:25:22 -06:00
|
|
|
if let ExprKind::Struct(qpath, fields, base) = expr.kind;
|
2021-03-12 08:30:50 -06:00
|
|
|
let ty = cx.typeck_results().expr_ty(expr);
|
2021-02-25 04:25:22 -06:00
|
|
|
if let Some(adt_def) = ty.ty_adt_def();
|
|
|
|
if adt_def.is_struct();
|
2022-03-04 14:28:41 -06:00
|
|
|
if let Some(variant) = adt_def.variants().iter().next();
|
2021-02-25 04:25:22 -06:00
|
|
|
if fields.iter().all(|f| f.is_shorthand);
|
|
|
|
then {
|
|
|
|
let mut def_order_map = FxHashMap::default();
|
|
|
|
for (idx, field) in variant.fields.iter().enumerate() {
|
2022-01-02 21:37:05 -06:00
|
|
|
def_order_map.insert(field.name, idx);
|
2021-02-25 04:25:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if is_consistent_order(fields, &def_order_map) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut ordered_fields: Vec<_> = fields.iter().map(|f| f.ident.name).collect();
|
|
|
|
ordered_fields.sort_unstable_by_key(|id| def_order_map[id]);
|
|
|
|
|
|
|
|
let mut fields_snippet = String::new();
|
|
|
|
let (last_ident, idents) = ordered_fields.split_last().unwrap();
|
|
|
|
for ident in idents {
|
2022-05-05 09:12:52 -05:00
|
|
|
let _ = write!(fields_snippet, "{}, ", ident);
|
2021-02-25 04:25:22 -06:00
|
|
|
}
|
|
|
|
fields_snippet.push_str(&last_ident.to_string());
|
|
|
|
|
|
|
|
let base_snippet = if let Some(base) = base {
|
|
|
|
format!(", ..{}", snippet(cx, base.span, ".."))
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
let sugg = format!("{} {{ {}{} }}",
|
|
|
|
snippet(cx, qpath.span(), ".."),
|
|
|
|
fields_snippet,
|
|
|
|
base_snippet,
|
|
|
|
);
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
INCONSISTENT_STRUCT_CONSTRUCTOR,
|
|
|
|
expr.span,
|
2021-03-25 13:29:11 -05:00
|
|
|
"struct constructor field order is inconsistent with struct definition field order",
|
2021-02-25 04:25:22 -06:00
|
|
|
"try",
|
|
|
|
sugg,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the order of the fields in the constructor is consistent with the order in the
|
|
|
|
// definition.
|
2021-03-15 16:36:07 -05:00
|
|
|
fn is_consistent_order<'tcx>(fields: &'tcx [hir::ExprField<'tcx>], def_order_map: &FxHashMap<Symbol, usize>) -> bool {
|
2021-02-25 04:25:22 -06:00
|
|
|
let mut cur_idx = usize::MIN;
|
|
|
|
for f in fields {
|
|
|
|
let next_idx = def_order_map[&f.ident.name];
|
|
|
|
if cur_idx > next_idx {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cur_idx = next_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|