Skip to content

add default_mismatches_new lint #14234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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 @@ -5607,6 +5607,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
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ pub static LINTS: &[&crate::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::DEFAULT_MISMATCHES_NEW_INFO,
crate::new_without_default::NEW_WITHOUT_DEFAULT_INFO,
crate::no_effect::NO_EFFECT_INFO,
crate::no_effect::NO_EFFECT_UNDERSCORE_BINDING_INFO,
Expand Down
13 changes: 3 additions & 10 deletions clippy_lints/src/default_constructed_unit_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,23 @@ declare_clippy_lint! {
}
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()
&& !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
356 changes: 268 additions & 88 deletions clippy_lints/src/new_without_default.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/ui/default_constructed_unit_structs.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(unused)]
#![allow(unused, clippy::default_mismatches_new)]
#![warn(clippy::default_constructed_unit_structs)]
use std::marker::PhantomData;

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/default_constructed_unit_structs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(unused)]
#![allow(unused, clippy::default_mismatches_new)]
#![warn(clippy::default_constructed_unit_structs)]
use std::marker::PhantomData;

Expand Down
166 changes: 166 additions & 0 deletions tests/ui/default_mismatches_new.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#![allow(clippy::needless_return, clippy::diverging_sub_expression)]
#![warn(clippy::default_mismatches_new)]

fn main() {}

//
// Nothing to change
//
struct ManualDefault(i32);
impl ManualDefault {
fn new() -> Self {
Self(42)
}
}
impl Default for ManualDefault {
fn default() -> Self {
Self(42)
}
}

#[derive(Default)]
struct CallToDefaultDefault(i32);
impl CallToDefaultDefault {
fn new() -> Self {
Default::default()
}
}

#[derive(Default)]
struct CallToSelfDefault(i32);
impl CallToSelfDefault {
fn new() -> Self {
Self::default()
}
}

#[derive(Default)]
struct CallToTypeDefault(i32);
impl CallToTypeDefault {
fn new() -> Self {
CallToTypeDefault::default()
}
}

#[derive(Default)]
struct CallToFullTypeDefault(i32);
impl CallToFullTypeDefault {
fn new() -> Self {
crate::CallToFullTypeDefault::default()
}
}

#[derive(Default)]
struct ReturnCallToSelfDefault(i32);
impl ReturnCallToSelfDefault {
fn new() -> Self {
return Self::default();
}
}

#[derive(Default)]
struct MakeResultSelf(i32);
impl MakeResultSelf {
fn new() -> Result<Self, ()> {
Ok(Self(10))
}
}

#[derive(Default)]
struct WithParams(i32);
impl WithParams {
fn new(val: i32) -> Self {
Self(val)
}
}

#[derive(Default)]
struct Async(i32);
impl Async {
async fn new() -> Self {
Self(42)
}
}

#[derive(Default)]
struct DeriveDefault;
impl DeriveDefault {
fn new() -> Self {
// Adding ::default() would cause clippy::default_constructed_unit_structs
Self
}
}

#[derive(Default)]
struct DeriveTypeDefault;
impl DeriveTypeDefault {
fn new() -> Self {
// Adding ::default() would cause clippy::default_constructed_unit_structs
return crate::DeriveTypeDefault;
}
}

//
// Offer suggestions
//

#[derive(Default)]
struct DeriveIntDefault {
value: i32,
}
impl DeriveIntDefault {
fn new() -> Self {
Self::default()
}
}

#[derive(Default)]
struct DeriveTupleDefault(i32);
impl DeriveTupleDefault {
fn new() -> Self {
Self::default()
}
}

#[derive(Default)]
struct NonZeroDeriveDefault(i32);
impl NonZeroDeriveDefault {
fn new() -> Self {
Self::default()
}
}

#[derive(Default)]
struct ExtraBlockDefault(i32);
impl ExtraBlockDefault {
fn new() -> Self {
Self::default()
}
}

#[derive(Default)]
struct ExtraBlockRetDefault(i32);
impl ExtraBlockRetDefault {
fn new() -> Self {
Self::default()
}
}

#[derive(Default)]
struct MultiStatements(i32);
impl MultiStatements {
fn new() -> Self {
Self::default()
}
}

//
// TODO: Fix in the future
//
#[derive(Default)]
struct OptionGeneric<T>(Option<T>);
impl<T> OptionGeneric<T> {
fn new() -> Self {
OptionGeneric(None)
}
}
Loading