Skip to content

Commit de5c437

Browse files
committed
fix(zero_repeat_side_effects): better identify exprs with side effects
1 parent b260f26 commit de5c437

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

clippy_lints/src/zero_repeat_side_effects.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::higher::VecArgs;
33
use clippy_utils::source::snippet;
4-
use clippy_utils::visitors::for_each_expr_without_closures;
54
use rustc_ast::LitKind;
65
use rustc_data_structures::packed::Pu128;
76
use rustc_errors::Applicability;
@@ -11,7 +10,7 @@ use rustc_session::declare_lint_pass;
1110

1211
declare_clippy_lint! {
1312
/// ### What it does
14-
/// Checks for array or vec initializations which call a function or method,
13+
/// Checks for array or vec initializations which contain an expression with side effects,
1514
/// but which have a repeat count of zero.
1615
///
1716
/// ### Why is this bad?
@@ -71,15 +70,7 @@ impl LateLintPass<'_> for ZeroRepeatSideEffects {
7170

7271
fn inner_check(cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>, inner_expr: &'_ rustc_hir::Expr<'_>, is_vec: bool) {
7372
// check if expr is a call or has a call inside it
74-
if for_each_expr_without_closures(inner_expr, |x| {
75-
if let ExprKind::Call(_, _) | ExprKind::MethodCall(_, _, _, _) = x.kind {
76-
std::ops::ControlFlow::Break(())
77-
} else {
78-
std::ops::ControlFlow::Continue(())
79-
}
80-
})
81-
.is_some()
82-
{
73+
if inner_expr.can_have_side_effects() {
8374
let parent_hir_node = cx.tcx.parent_hir_node(expr.hir_id);
8475
let return_type = cx.typeck_results().expr_ty(expr);
8576

@@ -107,7 +98,7 @@ fn inner_check(cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>, inner_expr:
10798
cx,
10899
ZERO_REPEAT_SIDE_EFFECTS,
109100
span.source_callsite(),
110-
"function or method calls as the initial value in zero-sized array initializers may cause side effects",
101+
"expression with side effects as the initial value in a zero-sized array initializer",
111102
|diag| {
112103
diag.span_suggestion_verbose(
113104
span.source_callsite(),

tests/ui/zero_repeat_side_effects.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,10 @@ fn issue_13110() {
7777
const LENGTH: usize = LEN!();
7878
let _data = [f(); LENGTH];
7979
}
80+
81+
fn issue_14681() {
82+
// should not trigger on non-function calls
83+
fn foo<T>(_s: &[Option<T>]) {}
84+
85+
foo(&[Some(0i64); 0]);
86+
}

tests/ui/zero_repeat_side_effects.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,10 @@ fn issue_13110() {
7777
const LENGTH: usize = LEN!();
7878
let _data = [f(); LENGTH];
7979
}
80+
81+
fn issue_14681() {
82+
// should not trigger on non-function calls
83+
fn foo<T>(_s: &[Option<T>]) {}
84+
85+
foo(&[Some(0i64); 0]);
86+
}

tests/ui/zero_repeat_side_effects.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: function or method calls as the initial value in zero-sized array initializers may cause side effects
1+
error: expression with side effects as the initial value in a zero-sized array initializer
22
--> tests/ui/zero_repeat_side_effects.rs:16:5
33
|
44
LL | let a = [f(); 0];
@@ -12,7 +12,7 @@ LL - let a = [f(); 0];
1212
LL + f(); let a: [i32; 0] = [];
1313
|
1414

15-
error: function or method calls as the initial value in zero-sized array initializers may cause side effects
15+
error: expression with side effects as the initial value in a zero-sized array initializer
1616
--> tests/ui/zero_repeat_side_effects.rs:19:5
1717
|
1818
LL | b = [f(); 0];
@@ -24,7 +24,7 @@ LL - b = [f(); 0];
2424
LL + f(); b = [] as [i32; 0];
2525
|
2626

27-
error: function or method calls as the initial value in zero-sized array initializers may cause side effects
27+
error: expression with side effects as the initial value in a zero-sized array initializer
2828
--> tests/ui/zero_repeat_side_effects.rs:24:5
2929
|
3030
LL | let c = vec![f(); 0];
@@ -36,7 +36,7 @@ LL - let c = vec![f(); 0];
3636
LL + f(); let c: std::vec::Vec<i32> = vec![];
3737
|
3838

39-
error: function or method calls as the initial value in zero-sized array initializers may cause side effects
39+
error: expression with side effects as the initial value in a zero-sized array initializer
4040
--> tests/ui/zero_repeat_side_effects.rs:27:5
4141
|
4242
LL | d = vec![f(); 0];
@@ -48,7 +48,7 @@ LL - d = vec![f(); 0];
4848
LL + f(); d = vec![] as std::vec::Vec<i32>;
4949
|
5050

51-
error: function or method calls as the initial value in zero-sized array initializers may cause side effects
51+
error: expression with side effects as the initial value in a zero-sized array initializer
5252
--> tests/ui/zero_repeat_side_effects.rs:31:5
5353
|
5454
LL | let e = [println!("side effect"); 0];
@@ -60,7 +60,7 @@ LL - let e = [println!("side effect"); 0];
6060
LL + println!("side effect"); let e: [(); 0] = [];
6161
|
6262

63-
error: function or method calls as the initial value in zero-sized array initializers may cause side effects
63+
error: expression with side effects as the initial value in a zero-sized array initializer
6464
--> tests/ui/zero_repeat_side_effects.rs:35:5
6565
|
6666
LL | let g = [{ f() }; 0];
@@ -72,7 +72,7 @@ LL - let g = [{ f() }; 0];
7272
LL + { f() }; let g: [i32; 0] = [];
7373
|
7474

75-
error: function or method calls as the initial value in zero-sized array initializers may cause side effects
75+
error: expression with side effects as the initial value in a zero-sized array initializer
7676
--> tests/ui/zero_repeat_side_effects.rs:39:10
7777
|
7878
LL | drop(vec![f(); 0]);
@@ -84,7 +84,7 @@ LL - drop(vec![f(); 0]);
8484
LL + drop({ f(); vec![] as std::vec::Vec<i32> });
8585
|
8686

87-
error: function or method calls as the initial value in zero-sized array initializers may cause side effects
87+
error: expression with side effects as the initial value in a zero-sized array initializer
8888
--> tests/ui/zero_repeat_side_effects.rs:43:5
8989
|
9090
LL | vec![f(); 0];
@@ -96,7 +96,7 @@ LL - vec![f(); 0];
9696
LL + { f(); vec![] as std::vec::Vec<i32> };
9797
|
9898

99-
error: function or method calls as the initial value in zero-sized array initializers may cause side effects
99+
error: expression with side effects as the initial value in a zero-sized array initializer
100100
--> tests/ui/zero_repeat_side_effects.rs:45:5
101101
|
102102
LL | [f(); 0];

0 commit comments

Comments
 (0)