Skip to content

Rustup #3711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 28, 2019
Merged

Rustup #3711

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clippy_lints/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc::{bug, span_bug};
use rustc_data_structures::sync::Lrc;
use std::cmp::Ordering::{self, Equal};
use std::cmp::PartialOrd;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::hash::{Hash, Hasher};
use syntax::ast::{FloatTy, LitKind};
Expand Down Expand Up @@ -441,12 +442,12 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
// FIXME: implement other conversion
_ => None,
},
ConstValue::ScalarPair(Scalar::Ptr(ptr), Scalar::Bits { bits: n, .. }) => match result.ty.sty {
ConstValue::Slice(Scalar::Ptr(ptr), n) => match result.ty.sty {
ty::Ref(_, tam, _) => match tam.sty {
ty::Str => {
let alloc = tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
let offset = ptr.offset.bytes().try_into().expect("too-large pointer offset");
let n = n as usize;
let n = usize::try_from(n).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling try_from() just to instantly unwrap it looks odd.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea it is, but it's similar to foo.checked_add(bar).unwrap() which prevents silent overflow irrelevant of the compiler flags.

String::from_utf8(alloc.bytes[offset..(offset + n)].to_owned())
.ok()
.map(Constant::Str)
Expand Down
15 changes: 0 additions & 15 deletions clippy_lints/src/replace_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
const REPLACEMENTS: &[(&[&str], &str)] = &[
// Once
(&["core", "sync", "ONCE_INIT"], "Once::new()"),
// Atomic
(
&["core", "sync", "atomic", "ATOMIC_BOOL_INIT"],
"AtomicBool::new(false)",
),
(&["core", "sync", "atomic", "ATOMIC_ISIZE_INIT"], "AtomicIsize::new(0)"),
(&["core", "sync", "atomic", "ATOMIC_I8_INIT"], "AtomicI8::new(0)"),
(&["core", "sync", "atomic", "ATOMIC_I16_INIT"], "AtomicI16::new(0)"),
(&["core", "sync", "atomic", "ATOMIC_I32_INIT"], "AtomicI32::new(0)"),
(&["core", "sync", "atomic", "ATOMIC_I64_INIT"], "AtomicI64::new(0)"),
(&["core", "sync", "atomic", "ATOMIC_USIZE_INIT"], "AtomicUsize::new(0)"),
(&["core", "sync", "atomic", "ATOMIC_U8_INIT"], "AtomicU8::new(0)"),
(&["core", "sync", "atomic", "ATOMIC_U16_INIT"], "AtomicU16::new(0)"),
(&["core", "sync", "atomic", "ATOMIC_U32_INIT"], "AtomicU32::new(0)"),
(&["core", "sync", "atomic", "ATOMIC_U64_INIT"], "AtomicU64::new(0)"),
// Min
(&["core", "isize", "MIN"], "isize::min_value()"),
(&["core", "i8", "MIN"], "i8::min_value()"),
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/utils/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ pub const INDEX: [&str; 3] = ["core", "ops", "Index"];
pub const INDEX_MUT: [&str; 3] = ["core", "ops", "IndexMut"];
pub const INIT: [&str; 4] = ["core", "intrinsics", "", "init"];
pub const INTO: [&str; 3] = ["core", "convert", "Into"];
pub const INTO_ITERATOR: [&str; 4] = ["core", "iter", "traits", "IntoIterator"];
pub const INTO_ITERATOR: [&str; 5] = ["core", "iter", "traits", "collect", "IntoIterator"];
pub const IO_READ: [&str; 3] = ["std", "io", "Read"];
pub const IO_WRITE: [&str; 3] = ["std", "io", "Write"];
pub const ITERATOR: [&str; 4] = ["core", "iter", "iterator", "Iterator"];
pub const ITERATOR: [&str; 5] = ["core", "iter", "traits", "iterator", "Iterator"];
pub const LATE_CONTEXT: [&str; 4] = ["rustc", "lint", "context", "LateContext"];
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
pub const LINT: [&str; 3] = ["rustc", "lint", "Lint"];
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/non_copy_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::borrow::Cow;
use std::cell::Cell;
use std::fmt::Display;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Once;

const ATOMIC: AtomicUsize = AtomicUsize::new(5); //~ ERROR interior mutable
Expand Down Expand Up @@ -95,9 +95,6 @@ fn main() {
ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability

ATOMIC_USIZE_INIT.store(2, Ordering::SeqCst); //~ ERROR interior mutability
assert_eq!(ATOMIC_USIZE_INIT.load(Ordering::SeqCst), 0); //~ ERROR interior mutability

let _once = ONCE_INIT;
let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
Expand Down
46 changes: 15 additions & 31 deletions tests/ui/non_copy_const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -144,132 +144,116 @@ LL | assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutabi
= 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/non_copy_const.rs:98:5
|
LL | ATOMIC_USIZE_INIT.store(2, 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/non_copy_const.rs:99:16
|
LL | assert_eq!(ATOMIC_USIZE_INIT.load(Ordering::SeqCst), 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/non_copy_const.rs:102:22
--> $DIR/non_copy_const.rs:99: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/non_copy_const.rs:103:25
--> $DIR/non_copy_const.rs:100: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/non_copy_const.rs:104:27
--> $DIR/non_copy_const.rs:101: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/non_copy_const.rs:105:26
--> $DIR/non_copy_const.rs:102: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/non_copy_const.rs:116:14
--> $DIR/non_copy_const.rs:113: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/non_copy_const.rs:117:14
--> $DIR/non_copy_const.rs:114: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/non_copy_const.rs:118:19
--> $DIR/non_copy_const.rs:115: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/non_copy_const.rs:119:14
--> $DIR/non_copy_const.rs:116: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/non_copy_const.rs:120:13
--> $DIR/non_copy_const.rs:117: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/non_copy_const.rs:126:13
--> $DIR/non_copy_const.rs:123: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/non_copy_const.rs:131:5
--> $DIR/non_copy_const.rs:128: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/non_copy_const.rs:132:16
--> $DIR/non_copy_const.rs:129: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/non_copy_const.rs:145:5
--> $DIR/non_copy_const.rs:142: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/non_copy_const.rs:146:16
--> $DIR/non_copy_const.rs:143:16
|
LL | assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
| ^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: aborting due to 31 previous errors
error: aborting due to 29 previous errors

12 changes: 0 additions & 12 deletions tests/ui/replace_consts.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ use std::sync::{Once, ONCE_INIT};
fn bad() {
// Once
{ let foo = ONCE_INIT; };
// Atomic
{ let foo = AtomicBool::new(false); };
{ let foo = AtomicIsize::new(0); };
{ let foo = AtomicI8::new(0); };
{ let foo = AtomicI16::new(0); };
{ let foo = AtomicI32::new(0); };
{ let foo = AtomicI64::new(0); };
{ let foo = AtomicUsize::new(0); };
{ let foo = AtomicU8::new(0); };
{ let foo = AtomicU16::new(0); };
{ let foo = AtomicU32::new(0); };
{ let foo = AtomicU64::new(0); };
// Min
{ let foo = isize::min_value(); };
{ let foo = i8::min_value(); };
Expand Down
12 changes: 0 additions & 12 deletions tests/ui/replace_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ use std::sync::{Once, ONCE_INIT};
fn bad() {
// Once
{ let foo = ONCE_INIT; };
// Atomic
{ let foo = ATOMIC_BOOL_INIT; };
{ let foo = ATOMIC_ISIZE_INIT; };
{ let foo = ATOMIC_I8_INIT; };
{ let foo = ATOMIC_I16_INIT; };
{ let foo = ATOMIC_I32_INIT; };
{ let foo = ATOMIC_I64_INIT; };
{ let foo = ATOMIC_USIZE_INIT; };
{ let foo = ATOMIC_U8_INIT; };
{ let foo = ATOMIC_U16_INIT; };
{ let foo = ATOMIC_U32_INIT; };
{ let foo = ATOMIC_U64_INIT; };
// Min
{ let foo = std::isize::MIN; };
{ let foo = std::i8::MIN; };
Expand Down
Loading