Skip to content

Commit 76b8434

Browse files
committed
ignore lints on indexing in const context
1 parent 518591f commit 76b8434

File tree

3 files changed

+6
-15
lines changed

3 files changed

+6
-15
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
266266
&& reduced.iter().all(|e| e.span.ctxt() == ctxt)
267267
{
268268
if let ExprKind::Index(..) = &expr.kind {
269+
if is_inside_always_const_context(cx.tcx, expr.hir_id) {
270+
return;
271+
}
269272
let snippet =
270273
if let (Some(arr), Some(func)) = (snippet_opt(cx, reduced[0].span), snippet_opt(cx, reduced[1].span)) {
271274
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)