Skip to content

Commit 1f693b4

Browse files
committed
do not lint on indexing inside const contexts
1 parent 9fa2779 commit 1f693b4

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
22
use clippy_utils::source::snippet_opt;
33
use clippy_utils::ty::has_drop;
4-
use clippy_utils::{any_parent_is_automatically_derived, is_lint_allowed, path_to_local, peel_blocks};
4+
use clippy_utils::{
5+
any_parent_is_automatically_derived, is_inside_always_const_context, is_lint_allowed, path_to_local, peel_blocks,
6+
};
57
use rustc_errors::Applicability;
68
use rustc_hir::def::{DefKind, Res};
79
use rustc_hir::{
@@ -265,6 +267,9 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
265267
&& reduced.iter().all(|e| e.span.ctxt() == ctxt)
266268
{
267269
if let ExprKind::Index(..) = &expr.kind {
270+
if is_inside_always_const_context(cx.tcx, expr.hir_id) {
271+
return;
272+
}
268273
let snippet =
269274
if let (Some(arr), Some(func)) = (snippet_opt(cx, reduced[0].span), snippet_opt(cx, reduced[1].span)) {
270275
format!("assert!({}.len() > {});", &arr, &func)

tests/ui/unnecessary_operation.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ fn main() {
114114
break 'label
115115
};
116116
let () = const {
117-
assert!([42, 55].len() > get_usize());
117+
[42, 55][get_usize()];
118118
};
119119
}
120120

121121
const _: () = {
122-
assert!([42, 55].len() > get_usize());
122+
[42, 55][get_usize()];
123123
};
124124

125125
const fn foo() {

tests/ui/unnecessary_operation.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,11 @@ LL | | s: String::from("blah"),
119119
LL | | };
120120
| |______^ help: statement can be reduced to: `String::from("blah");`
121121

122-
error: unnecessary operation
123-
--> tests/ui/unnecessary_operation.rs:121:9
124-
|
125-
LL | [42, 55][get_usize()];
126-
| ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`
127-
128-
error: unnecessary operation
129-
--> tests/ui/unnecessary_operation.rs:126:5
130-
|
131-
LL | [42, 55][get_usize()];
132-
| ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`
133-
134122
error: unnecessary operation
135123
--> tests/ui/unnecessary_operation.rs:130:5
136124
|
137125
LL | [42, 55][get_usize()];
138126
| ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`
139127

140-
error: aborting due to 22 previous errors
128+
error: aborting due to 20 previous errors
141129

0 commit comments

Comments
 (0)