Skip to content

Commit

Permalink
ignore lints on indexing in const context
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed May 26, 2024
1 parent 39f0667 commit f2f45dd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
3 changes: 3 additions & 0 deletions clippy_lints/src/no_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
&& reduced.iter().all(|e| e.span.ctxt() == ctxt)
{
if let ExprKind::Index(..) = &expr.kind {
if is_inside_always_const_context(cx.tcx, expr.hir_id) {
return;
}
let snippet =
if let (Some(arr), Some(func)) = (snippet_opt(cx, reduced[0].span), snippet_opt(cx, reduced[1].span)) {
format!("assert!({}.len() > {});", &arr, &func)
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/unnecessary_operation.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ fn main() {
break 'label
};
let () = const {
assert!([42, 55].len() > get_usize());
[42, 55][get_usize()];
};
}

const _: () = {
assert!([42, 55].len() > get_usize());
[42, 55][get_usize()];
};

const fn foo() {
Expand Down
14 changes: 1 addition & 13 deletions tests/ui/unnecessary_operation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,11 @@ LL | | s: String::from("blah"),
LL | | };
| |______^ help: statement can be reduced to: `String::from("blah");`

error: unnecessary operation
--> tests/ui/unnecessary_operation.rs:121:9
|
LL | [42, 55][get_usize()];
| ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`

error: unnecessary operation
--> tests/ui/unnecessary_operation.rs:126:5
|
LL | [42, 55][get_usize()];
| ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`

error: unnecessary operation
--> tests/ui/unnecessary_operation.rs:130:5
|
LL | [42, 55][get_usize()];
| ^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([42, 55].len() > get_usize());`

error: aborting due to 22 previous errors
error: aborting due to 20 previous errors

0 comments on commit f2f45dd

Please sign in to comment.