Skip to content
Merged
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
16 changes: 9 additions & 7 deletions compiler/rustc_mir_transform/src/known_panics_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,13 +600,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
}

Len(place) => {
let len = match self.get_const(place)? {
Value::Immediate(src) => src.len(&self.ecx).discard_err()?,
Value::Aggregate { fields, .. } => fields.len() as u64,
Value::Uninit => match place.ty(self.local_decls(), self.tcx).ty.kind() {
ty::Array(_, n) => n.try_eval_target_usize(self.tcx, self.param_env)?,
_ => return None,
},
let len = if let ty::Array(_, n) = place.ty(self.local_decls(), self.tcx).ty.kind()
{
n.try_eval_target_usize(self.tcx, self.param_env)?
} else {
match self.get_const(place)? {
Value::Immediate(src) => src.len(&self.ecx).discard_err()?,
Value::Aggregate { fields, .. } => fields.len() as u64,
Value::Uninit => return None,
}
};
ImmTy::from_scalar(Scalar::from_target_usize(len, self), layout).into()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ mod issue9612 {
util();
}

#[allow(unconditional_panic)]
fn util() {
let _a: u8 = 4.try_into().unwrap();
let _a: u8 = 5.try_into().expect("");
Expand Down
1 change: 1 addition & 0 deletions src/tools/clippy/tests/ui-toml/unwrap_used/unwrap_used.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ mod issue9612 {
util();
}

#[allow(unconditional_panic)]
fn util() {
let _a: u8 = 4.try_into().unwrap();
let _a: u8 = 5.try_into().expect("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ LL | let _ = &boxed_slice[1];
| ~~~~~~~~~~~~~~~

error: called `.get().unwrap()` on a slice
--> tests/ui-toml/unwrap_used/unwrap_used.rs:93:17
--> tests/ui-toml/unwrap_used/unwrap_used.rs:94:17
|
LL | let _ = Box::new([0]).get(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions src/tools/clippy/tests/ui/get_unwrap.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn main() {
mod issue9909 {
#![allow(clippy::identity_op, clippy::unwrap_used, dead_code)]

#[allow(unconditional_panic)]
fn reduced() {
let f = &[1, 2, 3];

Expand Down
1 change: 1 addition & 0 deletions src/tools/clippy/tests/ui/get_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn main() {
mod issue9909 {
#![allow(clippy::identity_op, clippy::unwrap_used, dead_code)]

#[allow(unconditional_panic)]
fn reduced() {
let f = &[1, 2, 3];

Expand Down
8 changes: 4 additions & 4 deletions src/tools/clippy/tests/ui/get_unwrap.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec();
= help: consider using `expect()` to provide a better panic message

error: called `.get().unwrap()` on a slice
--> tests/ui/get_unwrap.rs:77:24
--> tests/ui/get_unwrap.rs:78:24
|
LL | let _x: &i32 = f.get(1 + 2).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -277,7 +277,7 @@ LL | let _x: &i32 = &f[1 + 2];
| ~~~~~~~~~

error: called `.get().unwrap()` on a slice
--> tests/ui/get_unwrap.rs:80:18
--> tests/ui/get_unwrap.rs:81:18
|
LL | let _x = f.get(1 + 2).unwrap().to_string();
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -288,7 +288,7 @@ LL | let _x = f[1 + 2].to_string();
| ~~~~~~~~

error: called `.get().unwrap()` on a slice
--> tests/ui/get_unwrap.rs:83:18
--> tests/ui/get_unwrap.rs:84:18
|
LL | let _x = f.get(1 + 2).unwrap().abs();
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -299,7 +299,7 @@ LL | let _x = f[1 + 2].abs();
| ~~~~~~~~

error: called `.get_mut().unwrap()` on a slice
--> tests/ui/get_unwrap.rs:100:33
--> tests/ui/get_unwrap.rs:101:33
|
LL | let b = rest.get_mut(linidx(j, k) - linidx(i, k) - 1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lint/unconditional_panic_promoted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ build-fail

fn main() {
// MIR encodes this as a reborrow from a promoted constant.
// But the array lenth can still be gotten from the type.
let slice = &[0, 1];
let _ = slice[2]; //~ ERROR: this operation will panic at runtime [unconditional_panic]
}
10 changes: 10 additions & 0 deletions tests/ui/lint/unconditional_panic_promoted.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: this operation will panic at runtime
--> $DIR/unconditional_panic_promoted.rs:7:13
|
LL | let _ = slice[2];
| ^^^^^^^^ index out of bounds: the length is 2 but the index is 2
|
= note: `#[deny(unconditional_panic)]` on by default

error: aborting due to 1 previous error

Loading