Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6633,6 +6633,7 @@ Released 2018-09-13
[`declare_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
[`default_constructed_unit_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_constructed_unit_structs
[`default_instead_of_iter_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_instead_of_iter_empty
[`default_mismatches_new`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_mismatches_new
[`default_numeric_fallback`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_numeric_fallback
[`default_trait_access`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_trait_access
[`default_union_representation`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_union_representation
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
crate::needless_update::NEEDLESS_UPDATE_INFO,
crate::neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD_INFO,
crate::neg_multiply::NEG_MULTIPLY_INFO,
crate::new_without_default::NEW_WITHOUT_DEFAULT_INFO,
crate::new_vs_default::DEFAULT_MISMATCHES_NEW_INFO,
crate::new_vs_default::NEW_WITHOUT_DEFAULT_INFO,
crate::no_effect::NO_EFFECT_INFO,
crate::no_effect::NO_EFFECT_UNDERSCORE_BINDING_INFO,
crate::no_effect::UNNECESSARY_OPERATION_INFO,
Expand Down
20 changes: 5 additions & 15 deletions clippy_lints/src/default_constructed_unit_structs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_ty_alias;
use clippy_utils::source::SpanRangeExt as _;
use clippy_utils::ty::is_unit_struct;
use hir::ExprKind;
use hir::def::Res;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::declare_lint_pass;
use rustc_span::sym;

Expand Down Expand Up @@ -50,30 +50,20 @@ declare_lint_pass!(DefaultConstructedUnitStructs => [
DEFAULT_CONSTRUCTED_UNIT_STRUCTS,
]);

fn is_alias(ty: hir::Ty<'_>) -> bool {
if let hir::TyKind::Path(ref qpath) = ty.kind {
is_ty_alias(qpath)
} else {
false
}
}

impl LateLintPass<'_> for DefaultConstructedUnitStructs {
fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
if let ExprKind::Call(fn_expr, &[]) = expr.kind
// make sure we have a call to `Default::default`
&& let ExprKind::Path(ref qpath @ hir::QPath::TypeRelative(base, _)) = fn_expr.kind
// make sure this isn't a type alias:
// `<Foo as Bar>::Assoc` cannot be used as a constructor
&& !is_alias(*base)
&& !matches!(base.kind, hir::TyKind::Path(ref qpath) if is_ty_alias(qpath))
&& let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id)
&& cx.tcx.is_diagnostic_item(sym::default_fn, def_id)
// make sure we have a struct with no fields (unit struct)
&& let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind()
&& def.is_struct()
&& let var @ ty::VariantDef { ctor: Some((hir::def::CtorKind::Const, _)), .. } = def.non_enum_variant()
&& !var.is_field_list_non_exhaustive()
&& !expr.span.from_expansion() && !qpath.span().from_expansion()
&& is_unit_struct(cx.typeck_results().expr_ty(expr))
&& !expr.span.from_expansion()
&& !qpath.span().from_expansion()
// do not suggest replacing an expression by a type name with placeholders
&& !base.is_suggestable_infer_ty()
{
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ mod needless_question_mark;
mod needless_update;
mod neg_cmp_op_on_partial_ord;
mod neg_multiply;
mod new_without_default;
mod new_vs_default;
mod no_effect;
mod no_mangle_with_rust_abi;
mod non_canonical_impls;
Expand Down Expand Up @@ -616,7 +616,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
},
Box::new(|_| Box::new(swap::Swap)),
Box::new(|_| Box::new(panicking_overflow_checks::PanickingOverflowChecks)),
Box::new(|_| Box::<new_without_default::NewWithoutDefault>::default()),
Box::new(|_| Box::<new_vs_default::NewVsDefault>::default()),
Box::new(move |_| Box::new(disallowed_names::DisallowedNames::new(conf))),
Box::new(move |tcx| Box::new(functions::Functions::new(tcx, conf))),
Box::new(move |_| Box::new(doc::Documentation::new(conf))),
Expand Down
Loading
Loading