-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally allow
expect
and unwrap
in tests
- Loading branch information
Showing
12 changed files
with
543 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
allow-expect-in-tests = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// compile-flags: --test | ||
#![warn(clippy::expect_used)] | ||
|
||
fn expect_option() { | ||
let opt = Some(0); | ||
let _ = opt.expect(""); | ||
} | ||
|
||
fn expect_result() { | ||
let res: Result<u8, ()> = Ok(0); | ||
let _ = res.expect(""); | ||
} | ||
|
||
fn main() { | ||
expect_option(); | ||
expect_result(); | ||
} | ||
|
||
#[test] | ||
fn test_expect_option() { | ||
let opt = Some(0); | ||
let _ = opt.expect(""); | ||
} | ||
|
||
#[test] | ||
fn test_expect_result() { | ||
let res: Result<u8, ()> = Ok(0); | ||
let _ = res.expect(""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error: used `expect()` on `an Option` value | ||
--> $DIR/expect_used.rs:6:13 | ||
| | ||
LL | let _ = opt.expect(""); | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::expect-used` implied by `-D warnings` | ||
= help: if this value is an `None`, it will panic | ||
|
||
error: used `expect()` on `a Result` value | ||
--> $DIR/expect_used.rs:11:13 | ||
| | ||
LL | let _ = res.expect(""); | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= help: if this value is an `Err`, it will panic | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `enable-raw-pointer-heuristic-for-send`, `max-suggested-slice-pattern-length`, `await-holding-invalid-types`, `max-include-file-size`, `third-party` at line 5 column 1 | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `enable-raw-pointer-heuristic-for-send`, `max-suggested-slice-pattern-length`, `await-holding-invalid-types`, `max-include-file-size`, `allow-expect-in-tests`, `allow-unwrap-in-tests`, `third-party` at line 5 column 1 | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
allow-unwrap-in-tests = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// compile-flags: --test | ||
|
||
#![allow(unused_mut, clippy::from_iter_instead_of_collect)] | ||
#![warn(clippy::unwrap_used)] | ||
#![deny(clippy::get_unwrap)] | ||
|
||
use std::collections::BTreeMap; | ||
use std::collections::HashMap; | ||
use std::collections::VecDeque; | ||
use std::iter::FromIterator; | ||
|
||
struct GetFalsePositive { | ||
arr: [u32; 3], | ||
} | ||
|
||
impl GetFalsePositive { | ||
fn get(&self, pos: usize) -> Option<&u32> { | ||
self.arr.get(pos) | ||
} | ||
fn get_mut(&mut self, pos: usize) -> Option<&mut u32> { | ||
self.arr.get_mut(pos) | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); | ||
let mut some_slice = &mut [0, 1, 2, 3]; | ||
let mut some_vec = vec![0, 1, 2, 3]; | ||
let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect(); | ||
let mut some_hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]); | ||
let mut some_btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(1, 'a'), (2, 'b')]); | ||
let mut false_positive = GetFalsePositive { arr: [0, 1, 2] }; | ||
|
||
{ | ||
// Test `get().unwrap()` | ||
let _ = boxed_slice.get(1).unwrap(); | ||
let _ = some_slice.get(0).unwrap(); | ||
let _ = some_vec.get(0).unwrap(); | ||
let _ = some_vecdeque.get(0).unwrap(); | ||
let _ = some_hashmap.get(&1).unwrap(); | ||
let _ = some_btreemap.get(&1).unwrap(); | ||
#[allow(clippy::unwrap_used)] | ||
let _ = false_positive.get(0).unwrap(); | ||
// Test with deref | ||
let _: u8 = *boxed_slice.get(1).unwrap(); | ||
} | ||
|
||
{ | ||
// Test `get_mut().unwrap()` | ||
*boxed_slice.get_mut(0).unwrap() = 1; | ||
*some_slice.get_mut(0).unwrap() = 1; | ||
*some_vec.get_mut(0).unwrap() = 1; | ||
*some_vecdeque.get_mut(0).unwrap() = 1; | ||
// Check false positives | ||
#[allow(clippy::unwrap_used)] | ||
{ | ||
*some_hashmap.get_mut(&1).unwrap() = 'b'; | ||
*some_btreemap.get_mut(&1).unwrap() = 'b'; | ||
*false_positive.get_mut(0).unwrap() = 1; | ||
} | ||
} | ||
|
||
{ | ||
// Test `get().unwrap().foo()` and `get_mut().unwrap().bar()` | ||
let _ = some_vec.get(0..1).unwrap().to_vec(); | ||
let _ = some_vec.get_mut(0..1).unwrap().to_vec(); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); | ||
let _ = boxed_slice.get(1).unwrap(); | ||
} |
Oops, something went wrong.