Skip to content

Commit c6d3ae9

Browse files
committed
fix(zero_repeat_side_effects): don't suggest unsuggestable types
1 parent de5c437 commit c6d3ae9

6 files changed

+91
-7
lines changed

clippy_lints/src/zero_repeat_side_effects.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_data_structures::packed::Pu128;
66
use rustc_errors::Applicability;
77
use rustc_hir::{ConstArgKind, ExprKind, Node};
88
use rustc_lint::{LateContext, LateLintPass};
9+
use rustc_middle::ty::IsSuggestable;
910
use rustc_session::declare_lint_pass;
1011

1112
declare_clippy_lint! {
@@ -72,6 +73,7 @@ fn inner_check(cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>, inner_expr:
7273
// check if expr is a call or has a call inside it
7374
if inner_expr.can_have_side_effects() {
7475
let parent_hir_node = cx.tcx.parent_hir_node(expr.hir_id);
76+
let inner_expr_ty = cx.typeck_results().expr_ty(inner_expr);
7577
let return_type = cx.typeck_results().expr_ty(expr);
7678

7779
let inner_expr = snippet(cx, inner_expr.span.source_callsite(), "..");
@@ -94,18 +96,25 @@ fn inner_check(cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>, inner_expr:
9496
),
9597
_ => (expr.span, format!("{{ {inner_expr}; {vec}[] as {return_type} }}")),
9698
};
99+
let span = span.source_callsite();
97100
span_lint_and_then(
98101
cx,
99102
ZERO_REPEAT_SIDE_EFFECTS,
100-
span.source_callsite(),
103+
span,
101104
"expression with side effects as the initial value in a zero-sized array initializer",
102105
|diag| {
103-
diag.span_suggestion_verbose(
104-
span.source_callsite(),
105-
"consider performing the side effect separately",
106-
sugg,
107-
Applicability::Unspecified,
108-
);
106+
if (!inner_expr_ty.is_never() || cx.tcx.features().never_type())
107+
&& return_type.is_suggestable(cx.tcx, true)
108+
{
109+
diag.span_suggestion_verbose(
110+
span,
111+
"consider performing the side effect separately",
112+
sugg,
113+
Applicability::Unspecified,
114+
);
115+
} else {
116+
diag.span_help(span, "consider performing the side effect separately");
117+
}
109118
},
110119
);
111120
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![warn(clippy::zero_repeat_side_effects)]
2+
#![allow(clippy::diverging_sub_expression)]
3+
#![feature(never_type)]
4+
5+
fn issue_14998() {
6+
// nameable type thanks to `never_type` being enabled, suggest
7+
panic!(); let _data: [!; 0] = [];
8+
//~^ zero_repeat_side_effects
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![warn(clippy::zero_repeat_side_effects)]
2+
#![allow(clippy::diverging_sub_expression)]
3+
#![feature(never_type)]
4+
5+
fn issue_14998() {
6+
// nameable type thanks to `never_type` being enabled, suggest
7+
let _data = [panic!(); 0];
8+
//~^ zero_repeat_side_effects
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: expression with side effects as the initial value in a zero-sized array initializer
2+
--> tests/ui/zero_repeat_side_effects_never_pattern.rs:7:5
3+
|
4+
LL | let _data = [panic!(); 0];
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::zero-repeat-side-effects` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::zero_repeat_side_effects)]`
9+
help: consider performing the side effect separately
10+
|
11+
LL - let _data = [panic!(); 0];
12+
LL + panic!(); let _data: [!; 0] = [];
13+
|
14+
15+
error: aborting due to 1 previous error
16+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@no-rustfix
2+
#![warn(clippy::zero_repeat_side_effects)]
3+
#![allow(clippy::diverging_sub_expression)]
4+
5+
fn issue_14998() {
6+
// unnameable types, don't suggest
7+
let _data = [|| 3i32; 0];
8+
//~^ zero_repeat_side_effects
9+
10+
// unnameable type because `never_type` is not enabled, don't suggest
11+
let _data = [panic!(); 0];
12+
//~^ zero_repeat_side_effects
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: expression with side effects as the initial value in a zero-sized array initializer
2+
--> tests/ui/zero_repeat_side_effects_unfixable.rs:7:5
3+
|
4+
LL | let _data = [|| 3i32; 0];
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
help: consider performing the side effect separately
8+
--> tests/ui/zero_repeat_side_effects_unfixable.rs:7:5
9+
|
10+
LL | let _data = [|| 3i32; 0];
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: `-D clippy::zero-repeat-side-effects` implied by `-D warnings`
13+
= help: to override `-D warnings` add `#[allow(clippy::zero_repeat_side_effects)]`
14+
15+
error: expression with side effects as the initial value in a zero-sized array initializer
16+
--> tests/ui/zero_repeat_side_effects_unfixable.rs:11:5
17+
|
18+
LL | let _data = [panic!(); 0];
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
help: consider performing the side effect separately
22+
--> tests/ui/zero_repeat_side_effects_unfixable.rs:11:5
23+
|
24+
LL | let _data = [panic!(); 0];
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
error: aborting due to 2 previous errors
28+

0 commit comments

Comments
 (0)