Split out tests

This commit is contained in:
Heinz N. Gies 2019-10-16 19:43:26 +02:00
parent a7ad78f3eb
commit 7f454d8d06
18 changed files with 245 additions and 171 deletions

View file

@ -1129,6 +1129,7 @@ Released 2018-09-13
[`ok_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#ok_expect
[`op_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#op_ref
[`option_and_then_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_and_then_some
[`option_expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_expect_used
[`option_map_or_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_or_none
[`option_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn
[`option_map_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unwrap_or
@ -1168,6 +1169,7 @@ Released 2018-09-13
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro
[`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts
[`result_expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_expect_used
[`result_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_unit_fn
[`result_map_unwrap_or_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_unwrap_or_else
[`result_unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_unwrap_used

View file

@ -6,7 +6,7 @@
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
[There are 329 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are 331 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:

View file

@ -625,7 +625,9 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
mem_forget::MEM_FORGET,
methods::CLONE_ON_REF_PTR,
methods::GET_UNWRAP,
methods::OPTION_EXPECT_USED,
methods::OPTION_UNWRAP_USED,
methods::RESULT_EXPECT_USED,
methods::RESULT_UNWRAP_USED,
methods::WRONG_PUB_SELF_CONVENTION,
misc::FLOAT_CMP_CONST,

View file

@ -111,9 +111,10 @@ declare_clippy_lint! {
///
/// Better:
///
/// ```rust
/// ```ignore
/// let opt = Some(1);
/// opt?;
/// # Some::<()>(())
/// ```
pub OPTION_EXPECT_USED,
restriction,
@ -139,9 +140,10 @@ declare_clippy_lint! {
///
/// Better:
///
/// ```rust
/// ```
/// let res: Result<usize, ()> = Ok(1);
/// res?;
/// # Ok::<(), ()>(())
/// ```
pub RESULT_EXPECT_USED,
restriction,

View file

@ -38,7 +38,7 @@ declare_clippy_lint! {
/// ```
pub PANIC,
restriction,
"missing parameters in `panic!` calls"
"usage of the `panic!` macro"
}
declare_clippy_lint! {

View file

@ -6,7 +6,7 @@ pub use lint::Lint;
pub use lint::LINT_LEVELS;
// begin lint list, do not remove this comment, its used in `update_lints`
pub const ALL_LINTS: [Lint; 329] = [
pub const ALL_LINTS: [Lint; 331] = [
Lint {
name: "absurd_extreme_comparisons",
group: "correctness",
@ -1393,6 +1393,13 @@ pub const ALL_LINTS: [Lint; 329] = [
deprecation: None,
module: "methods",
},
Lint {
name: "option_expect_used",
group: "restriction",
desc: "using `Option.expect()`, which might be better handled",
deprecation: None,
module: "methods",
},
Lint {
name: "option_map_or_none",
group: "style",
@ -1459,7 +1466,7 @@ pub const ALL_LINTS: [Lint; 329] = [
Lint {
name: "panic",
group: "restriction",
desc: "missing parameters in `panic!` calls",
desc: "usage of the `panic!` macro",
deprecation: None,
module: "panic_unimplemented",
},
@ -1659,6 +1666,13 @@ pub const ALL_LINTS: [Lint; 329] = [
deprecation: None,
module: "replace_consts",
},
Lint {
name: "result_expect_used",
group: "restriction",
desc: "using `Result.expect()`, which might be better handled",
deprecation: None,
module: "methods",
},
Lint {
name: "result_map_unit_fn",
group: "complexity",

16
tests/ui/expect.rs Normal file
View file

@ -0,0 +1,16 @@
#![warn(clippy::option_expect_used, clippy::result_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();
}

18
tests/ui/expect.stderr Normal file
View file

@ -0,0 +1,18 @@
error: used expect() on an Option value. If this value is an None it will panic
--> $DIR/expect.rs:5:13
|
LL | let _ = opt.expect("");
| ^^^^^^^^^^^^^^
|
= note: `-D clippy::option-expect-used` implied by `-D warnings`
error: used expect() on a Result value. If this value is an Err it will panic
--> $DIR/expect.rs:10:13
|
LL | let _ = res.expect("");
| ^^^^^^^^^^^^^^
|
= note: `-D clippy::result-expect-used` implied by `-D warnings`
error: aborting due to 2 previous errors

View file

@ -1,13 +1,7 @@
// aux-build:option_helpers.rs
// compile-flags: --edition 2018
#![warn(
clippy::all,
clippy::pedantic,
clippy::option_unwrap_used,
clippy::option_expect_used,
clippy::result_expect_used
)]
#![warn(clippy::all, clippy::pedantic)]
#![allow(
clippy::blacklisted_name,
clippy::default_trait_access,
@ -307,8 +301,8 @@ fn search_is_some() {
let _ = foo.rposition().is_some();
}
#[allow(clippy::similar_names)]
fn main() {
let opt = Some(0);
let _ = opt.unwrap();
option_methods();
filter_next();
search_is_some();
}

View file

@ -206,13 +206,5 @@ LL | | }
LL | | ).is_some();
| |______________________________^
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
--> $DIR/methods.rs:307:13
|
LL | let _ = opt.unwrap();
| ^^^^^^^^^^^^
|
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
error: aborting due to 24 previous errors
error: aborting due to 23 previous errors

View file

@ -1,12 +1,61 @@
#![warn(clippy::panic)]
#![warn(clippy::panic_params)]
#![allow(clippy::assertions_on_constants)]
fn missing() {
if true {
panic!("{}");
} else if false {
panic!("{:?}");
} else {
assert!(true, "here be missing values: {}");
}
fn panic() {
let a = 2;
panic!();
let b = a + 2;
panic!("{{{this}}}");
}
fn ok_single() {
panic!("foo bar");
}
fn ok_inner() {
// Test for #768
assert!("foo bar".contains(&format!("foo {}", "bar")));
}
fn ok_multiple() {
panic!("{}", "This is {ok}");
}
fn ok_bracket() {
match 42 {
1337 => panic!("{so is this"),
666 => panic!("so is this}"),
_ => panic!("}so is that{"),
}
}
const ONE: u32 = 1;
fn ok_nomsg() {
assert!({ 1 == ONE });
assert!(if 1 == ONE { ONE == 1 } else { false });
}
fn ok_escaped() {
panic!("{{ why should this not be ok? }}");
panic!(" or {{ that ?");
panic!(" or }} this ?");
panic!(" {or {{ that ?");
panic!(" }or }} this ?");
panic!("{{ test }");
panic!("{case }}");
}
fn main() {
panic();
missing();
ok_single();
ok_multiple();
ok_bracket();
ok_inner();
ok_nomsg();
ok_escaped();
}

View file

@ -1,10 +1,28 @@
error: `panic` should not be present in production code
--> $DIR/panic.rs:6:5
error: you probably are missing some parameter in your format string
--> $DIR/panic.rs:5:16
|
LL | panic!();
| ^^^^^^^^^
LL | panic!("{}");
| ^^^^
|
= note: `-D clippy::panic` implied by `-D warnings`
= note: `-D clippy::panic-params` implied by `-D warnings`
error: aborting due to previous error
error: you probably are missing some parameter in your format string
--> $DIR/panic.rs:7:16
|
LL | panic!("{:?}");
| ^^^^^^
error: you probably are missing some parameter in your format string
--> $DIR/panic.rs:9:23
|
LL | assert!(true, "here be missing values: {}");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: you probably are missing some parameter in your format string
--> $DIR/panic.rs:12:12
|
LL | panic!("{{{this}}}");
| ^^^^^^^^^^^^
error: aborting due to 4 previous errors

View file

@ -1,82 +0,0 @@
#![warn(clippy::panic_params, clippy::unimplemented, clippy::unreachable, clippy::todo)]
#![allow(clippy::assertions_on_constants)]
fn missing() {
if true {
panic!("{}");
} else if false {
panic!("{:?}");
} else {
assert!(true, "here be missing values: {}");
}
panic!("{{{this}}}");
}
fn ok_single() {
panic!("foo bar");
}
fn ok_inner() {
// Test for #768
assert!("foo bar".contains(&format!("foo {}", "bar")));
}
fn ok_multiple() {
panic!("{}", "This is {ok}");
}
fn ok_bracket() {
match 42 {
1337 => panic!("{so is this"),
666 => panic!("so is this}"),
_ => panic!("}so is that{"),
}
}
const ONE: u32 = 1;
fn ok_nomsg() {
assert!({ 1 == ONE });
assert!(if 1 == ONE { ONE == 1 } else { false });
}
fn ok_escaped() {
panic!("{{ why should this not be ok? }}");
panic!(" or {{ that ?");
panic!(" or }} this ?");
panic!(" {or {{ that ?");
panic!(" }or }} this ?");
panic!("{{ test }");
panic!("{case }}");
}
fn unimplemented() {
let a = 2;
unimplemented!();
let b = a + 2;
}
fn unreachable() {
let a = 2;
unreachable!();
let b = a + 2;
}
fn todo() {
let a = 2;
todo!();
let b = a + 2;
}
fn main() {
missing();
ok_single();
ok_multiple();
ok_bracket();
ok_inner();
ok_nomsg();
ok_escaped();
unimplemented();
unreachable();
todo();
}

View file

@ -1,52 +0,0 @@
error: you probably are missing some parameter in your format string
--> $DIR/panic_unimplemented.rs:5:16
|
LL | panic!("{}");
| ^^^^
|
= note: `-D clippy::panic-params` implied by `-D warnings`
error: you probably are missing some parameter in your format string
--> $DIR/panic_unimplemented.rs:7:16
|
LL | panic!("{:?}");
| ^^^^^^
error: you probably are missing some parameter in your format string
--> $DIR/panic_unimplemented.rs:9:23
|
LL | assert!(true, "here be missing values: {}");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: you probably are missing some parameter in your format string
--> $DIR/panic_unimplemented.rs:12:12
|
LL | panic!("{{{this}}}");
| ^^^^^^^^^^^^
error: `unimplemented` should not be present in production code
--> $DIR/panic_unimplemented.rs:55:5
|
LL | unimplemented!();
| ^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::unimplemented` implied by `-D warnings`
error: `unreachable` should not be present in production code
--> $DIR/panic_unimplemented.rs:61:5
|
LL | unreachable!();
| ^^^^^^^^^^^^^^^
|
= note: `-D clippy::unreachable` implied by `-D warnings`
error: `todo` should not be present in production code
--> $DIR/panic_unimplemented.rs:67:5
|
LL | todo!();
| ^^^^^^^^
|
= note: `-D clippy::todo` implied by `-D warnings`
error: aborting due to 7 previous errors

View file

@ -0,0 +1,33 @@
#![warn(clippy::unimplemented, clippy::unreachable, clippy::todo, clippy::panic)]
#![allow(clippy::assertions_on_constants)]
fn panic() {
let a = 2;
panic!();
let b = a + 2;
}
fn todo() {
let a = 2;
todo!();
let b = a + 2;
}
fn unimplemented() {
let a = 2;
unimplemented!();
let b = a + 2;
}
fn unreachable() {
let a = 2;
unreachable!();
let b = a + 2;
}
fn main() {
panic();
todo();
unimplemented();
unreachable();
}

View file

@ -0,0 +1,34 @@
error: `panic` should not be present in production code
--> $DIR/panicking_macros.rs:6:5
|
LL | panic!();
| ^^^^^^^^^
|
= note: `-D clippy::panic` implied by `-D warnings`
error: `todo` should not be present in production code
--> $DIR/panicking_macros.rs:12:5
|
LL | todo!();
| ^^^^^^^^
|
= note: `-D clippy::todo` implied by `-D warnings`
error: `unimplemented` should not be present in production code
--> $DIR/panicking_macros.rs:18:5
|
LL | unimplemented!();
| ^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::unimplemented` implied by `-D warnings`
error: `unreachable` should not be present in production code
--> $DIR/panicking_macros.rs:24:5
|
LL | unreachable!();
| ^^^^^^^^^^^^^^^
|
= note: `-D clippy::unreachable` implied by `-D warnings`
error: aborting due to 4 previous errors

16
tests/ui/unwrap.rs Normal file
View file

@ -0,0 +1,16 @@
#![warn(clippy::option_unwrap_used, clippy::result_unwrap_used)]
fn unwrap_option() {
let opt = Some(0);
let _ = opt.unwrap();
}
fn unwrap_result() {
let res: Result<u8, ()> = Ok(0);
let _ = res.unwrap();
}
fn main() {
unwrap_option();
unwrap_result();
}

18
tests/ui/unwrap.stderr Normal file
View file

@ -0,0 +1,18 @@
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
--> $DIR/unwrap.rs:5:13
|
LL | let _ = opt.unwrap();
| ^^^^^^^^^^^^
|
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
error: used unwrap() on a Result value. If you don't want to handle the Err case gracefully, consider using expect() to provide a better panic message
--> $DIR/unwrap.rs:10:13
|
LL | let _ = res.unwrap();
| ^^^^^^^^^^^^
|
= note: `-D clippy::result-unwrap-used` implied by `-D warnings`
error: aborting due to 2 previous errors