Skip to content

Commit

Permalink
Adjust Clippy for CONST_ITEM_MUTATION lint
Browse files Browse the repository at this point in the history
We no longer lint assignments to const item fields in the
`temporary_assignment` lint, since this is now covered by the
`CONST_ITEM_MUTATION` lint.

Additionally, we `#![allow(const_item_mutation)]` in the
`borrow_interior_mutable_const.rs` test. Clippy UI tests are run with
`-D warnings`, which seems to cause builtin lints to prevent Clippy
lints from running.
  • Loading branch information
Aaron1011 committed Sep 8, 2020
1 parent 4286d9c commit f23670e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 48 deletions.
4 changes: 1 addition & 3 deletions clippy_lints/src/temporary_assignment.rs
@@ -1,5 +1,4 @@
use crate::utils::{is_adjusted, span_lint};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand All @@ -22,10 +21,9 @@ declare_clippy_lint! {
"assignments to temporaries"
}

fn is_temporary(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
fn is_temporary(_cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
match &expr.kind {
ExprKind::Struct(..) | ExprKind::Tup(..) => true,
ExprKind::Path(qpath) => matches!(cx.qpath_res(qpath, expr.hir_id), Res::Def(DefKind::Const, ..)),
_ => false,
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/borrow_interior_mutable_const.rs
@@ -1,5 +1,6 @@
#![warn(clippy::borrow_interior_mutable_const)]
#![allow(clippy::declare_interior_mutable_const, clippy::ref_in_deref)]
#![allow(const_item_mutation)]

use std::borrow::Cow;
use std::cell::{Cell, UnsafeCell};
Expand Down
32 changes: 16 additions & 16 deletions tests/ui/borrow_interior_mutable_const.stderr
@@ -1,5 +1,5 @@
error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:65:5
--> $DIR/borrow_interior_mutable_const.rs:66:5
|
LL | ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
| ^^^^^^
Expand All @@ -8,119 +8,119 @@ LL | ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:66:16
--> $DIR/borrow_interior_mutable_const.rs:67:16
|
LL | assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
| ^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:69:22
--> $DIR/borrow_interior_mutable_const.rs:70:22
|
LL | let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
| ^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:70:25
--> $DIR/borrow_interior_mutable_const.rs:71:25
|
LL | let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
| ^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:71:27
--> $DIR/borrow_interior_mutable_const.rs:72:27
|
LL | let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
| ^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:72:26
--> $DIR/borrow_interior_mutable_const.rs:73:26
|
LL | let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
| ^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:83:14
--> $DIR/borrow_interior_mutable_const.rs:84:14
|
LL | let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
| ^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:84:14
--> $DIR/borrow_interior_mutable_const.rs:85:14
|
LL | let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
| ^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:85:19
--> $DIR/borrow_interior_mutable_const.rs:86:19
|
LL | let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
| ^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:86:14
--> $DIR/borrow_interior_mutable_const.rs:87:14
|
LL | let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
| ^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:87:13
--> $DIR/borrow_interior_mutable_const.rs:88:13
|
LL | let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
| ^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:93:13
--> $DIR/borrow_interior_mutable_const.rs:94:13
|
LL | let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
| ^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:98:5
--> $DIR/borrow_interior_mutable_const.rs:99:5
|
LL | CELL.set(2); //~ ERROR interior mutability
| ^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:99:16
--> $DIR/borrow_interior_mutable_const.rs:100:16
|
LL | assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
| ^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:112:5
--> $DIR/borrow_interior_mutable_const.rs:113:5
|
LL | u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
| ^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> $DIR/borrow_interior_mutable_const.rs:113:16
--> $DIR/borrow_interior_mutable_const.rs:114:16
|
LL | assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
| ^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions tests/ui/temporary_assignment.rs
@@ -1,4 +1,5 @@
#![warn(clippy::temporary_assignment)]
#![allow(const_item_mutation)]

use std::ops::{Deref, DerefMut};

Expand Down
34 changes: 5 additions & 29 deletions tests/ui/temporary_assignment.stderr
@@ -1,13 +1,13 @@
error: assignment to temporary
--> $DIR/temporary_assignment.rs:47:5
--> $DIR/temporary_assignment.rs:48:5
|
LL | Struct { field: 0 }.field = 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::temporary-assignment` implied by `-D warnings`

error: assignment to temporary
--> $DIR/temporary_assignment.rs:48:5
--> $DIR/temporary_assignment.rs:49:5
|
LL | / MultiStruct {
LL | | structure: Struct { field: 0 },
Expand All @@ -17,40 +17,16 @@ LL | | .field = 1;
| |______________^

error: assignment to temporary
--> $DIR/temporary_assignment.rs:53:5
--> $DIR/temporary_assignment.rs:54:5
|
LL | ArrayStruct { array: [0] }.array[0] = 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: assignment to temporary
--> $DIR/temporary_assignment.rs:54:5
--> $DIR/temporary_assignment.rs:55:5
|
LL | (0, 0).0 = 1;
| ^^^^^^^^^^^^

error: assignment to temporary
--> $DIR/temporary_assignment.rs:56:5
|
LL | A.0 = 2;
| ^^^^^^^

error: assignment to temporary
--> $DIR/temporary_assignment.rs:57:5
|
LL | B.field = 2;
| ^^^^^^^^^^^

error: assignment to temporary
--> $DIR/temporary_assignment.rs:58:5
|
LL | C.structure.field = 2;
| ^^^^^^^^^^^^^^^^^^^^^

error: assignment to temporary
--> $DIR/temporary_assignment.rs:59:5
|
LL | D.array[0] = 2;
| ^^^^^^^^^^^^^^

error: aborting due to 8 previous errors
error: aborting due to 4 previous errors

0 comments on commit f23670e

Please sign in to comment.