Skip to content

Commit d8ec7df

Browse files
committed
HasPlaceholder for None
1 parent 80beb86 commit d8ec7df

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

clippy_lints/src/trivial_default_constructed_types.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ impl<'tcx> LateLintPass<'tcx> for TrivialDefaultConstructedTypes {
3535
&& let ExprKind::Call(call, _) = expr.kind
3636
&& is_trait_item(cx, call, sym::Default)
3737
{
38+
let mut app = Applicability::MachineApplicable;
3839
let ret_ty = cx
3940
.typeck_results()
4041
.expr_ty(call)
4142
.fn_sig(cx.tcx)
4243
.output()
4344
.skip_binder()
4445
.peel_refs();
45-
if let Some(default) = default_value(cx, ret_ty) && !is_from_proc_macro(cx, expr) {
46+
if let Some(default) = default_value(cx, ret_ty, &mut app) && !is_from_proc_macro(cx, expr) {
4647
span_lint_and_sugg(
4748
cx,
4849
TRIVIAL_DEFAULT_CONSTRUCTED_TYPES,
@@ -55,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialDefaultConstructedTypes {
5556
} else if let ty::Tuple(fields) = ret_ty.kind()
5657
&& fields.len() <= 3
5758
&& let Some(fields_default) = fields.iter()
58-
.map(|field| default_value(cx, field))
59+
.map(|field| default_value(cx, field, &mut app))
5960
.collect::<Option<Vec<_>>>()
6061
&& !is_from_proc_macro(cx, expr)
6162
{
@@ -79,7 +80,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialDefaultConstructedTypes {
7980
Applicability::MachineApplicable,
8081
);
8182
} else if let ty::Array(ty, len) = ret_ty.kind()
82-
&& let Some(default) = default_value(cx, *ty)
83+
&& let Some(default) = default_value(cx, *ty, &mut app)
8384
&& !is_from_proc_macro(cx, expr)
8485
{
8586
span_lint_and_sugg(
@@ -89,18 +90,21 @@ impl<'tcx> LateLintPass<'tcx> for TrivialDefaultConstructedTypes {
8990
"constructing a trivial array using `default`",
9091
"try",
9192
format!("[{default}; {len}]"),
92-
Applicability::MachineApplicable,
93+
app,
9394
);
9495
}
9596
}
9697
}
9798
}
9899

99100
/// Gets the default value of `ty`.
100-
fn default_value(cx: &LateContext<'_>, ty: Ty<'_>) -> Option<Cow<'static, str>> {
101+
fn default_value(cx: &LateContext<'_>, ty: Ty<'_>, app: &mut Applicability) -> Option<Cow<'static, str>> {
101102
match ty.kind() {
102-
ty::Adt(def, substs) if let [subst] = substs.as_slice() => {
103-
is_lang_item_or_ctor(cx, def.did(), LangItem::Option).then(|| format!("None::<{subst}>").into())
103+
ty::Adt(def, _) => {
104+
*app = Applicability::HasPlaceholders;
105+
// Checking if the generic argument is required would substantially increase the
106+
// complexity of this lint, for now, just use a placeholder (`_`).
107+
is_lang_item_or_ctor(cx, def.did(), LangItem::Option).then(|| "None::<_>".into())
104108
},
105109
ty::Bool => Some("false".into()),
106110
ty::Str => Some(r#""""#.into()),

tests/ui/trivial_default_constructed_types.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern crate proc_macros;
88

99
fn main() {
1010
0u32;
11-
let x: Option<u32> = None::<u32>;
11+
let x: Option<u32> = None::<_>;
1212
let y: (usize,) = (0usize,);
1313
();
1414
let x: [u32; 10] = [0u32; 10];

tests/ui/trivial_default_constructed_types.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: constructing a trivial type using `default`
1010
--> $DIR/trivial_default_constructed_types.rs:11:26
1111
|
1212
LL | let x: Option<u32> = Option::default();
13-
| ^^^^^^^^^^^^^^^^^ help: try: `None::<u32>`
13+
| ^^^^^^^^^^^^^^^^^ help: try: `None::<_>`
1414

1515
error: constructing a trivial tuple using `default`
1616
--> $DIR/trivial_default_constructed_types.rs:12:23

0 commit comments

Comments
 (0)