New lint for struct update that has no effect

This commit is contained in:
Seo Sanghyeon 2015-10-22 00:25:16 +09:00
parent 1555a9f8af
commit d843257643
4 changed files with 56 additions and 1 deletions

View file

@ -51,6 +51,7 @@ pub mod mutex_atomic;
pub mod zero_div_zero;
pub mod open_options;
pub mod needless_features;
pub mod needless_update;
mod reexport {
pub use syntax::ast::{Name, Ident, NodeId};
@ -96,6 +97,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_late_lint_pass(box zero_div_zero::ZeroDivZeroPass);
reg.register_late_lint_pass(box mutex_atomic::MutexAtomic);
reg.register_late_lint_pass(box needless_features::NeedlessFeaturesPass);
reg.register_late_lint_pass(box needless_update::NeedlessUpdatePass);
reg.register_lint_group("clippy_pedantic", vec![
methods::OPTION_UNWRAP_USED,
@ -155,6 +157,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
needless_bool::NEEDLESS_BOOL,
needless_features::UNSTABLE_AS_MUT_SLICE,
needless_features::UNSTABLE_AS_SLICE,
needless_update::NEEDLESS_UPDATE,
open_options::NONSENSICAL_OPEN_OPTIONS,
precedence::PRECEDENCE,
ptr_arg::PTR_ARG,

35
src/needless_update.rs Normal file
View file

@ -0,0 +1,35 @@
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::middle::ty::TyStruct;
use rustc_front::hir::{Expr, ExprStruct};
use utils::span_lint;
declare_lint! {
pub NEEDLESS_UPDATE,
Warn,
"using `{ ..base }` when there are no missing fields"
}
#[derive(Copy, Clone)]
pub struct NeedlessUpdatePass;
impl LintPass for NeedlessUpdatePass {
fn get_lints(&self) -> LintArray {
lint_array!(NEEDLESS_UPDATE)
}
}
impl LateLintPass for NeedlessUpdatePass {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
if let ExprStruct(_, ref fields, Some(ref base)) = expr.node {
let ty = cx.tcx.expr_ty(expr);
if let TyStruct(def, _) = ty.sty {
if fields.len() == def.struct_variant().fields.len() {
span_lint(cx, NEEDLESS_UPDATE, base.span,
"struct update has no effect, all the fields \
in the struct have already been specified");
}
}
}
}
}