Merge option_unwrap_used and result_unwrap_used lints into unwrap_used lint

This commit is contained in:
ThibsG 2020-05-03 13:11:18 +02:00 committed by Thibaud Genty
parent 6cbdd1e49d
commit bcf61666bd
6 changed files with 37 additions and 58 deletions

View File

@ -1501,7 +1501,6 @@ Released 2018-09-13
[`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_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_option
[`option_unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_unwrap_used
[`or_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
[`out_of_bounds_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#out_of_bounds_indexing
[`overflow_check_conditional`]: https://rust-lang.github.io/rust-clippy/master/index.html#overflow_check_conditional
@ -1622,6 +1621,7 @@ Released 2018-09-13
[`unused_label`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_label
[`unused_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_self
[`unused_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit
[`unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
[`use_debug`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_debug
[`use_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_self
[`used_underscore_binding`]: https://rust-lang.github.io/rust-clippy/master/index.html#used_underscore_binding

View File

@ -680,11 +680,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&methods::OPTION_AS_REF_DEREF,
&methods::OPTION_EXPECT_USED,
&methods::OPTION_MAP_OR_NONE,
&methods::OPTION_UNWRAP_USED,
&methods::OR_FUN_CALL,
&methods::RESULT_EXPECT_USED,
&methods::RESULT_MAP_OR_INTO_OPTION,
&methods::RESULT_UNWRAP_USED,
&methods::SEARCH_IS_SOME,
&methods::SHOULD_IMPLEMENT_TRAIT,
&methods::SINGLE_CHAR_PATTERN,
@ -695,6 +693,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&methods::UNINIT_ASSUMED_INIT,
&methods::UNNECESSARY_FILTER_MAP,
&methods::UNNECESSARY_FOLD,
&methods::UNWRAP_USED,
&methods::USELESS_ASREF,
&methods::WRONG_PUB_SELF_CONVENTION,
&methods::WRONG_SELF_CONVENTION,
@ -1090,9 +1089,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&methods::FILETYPE_IS_FILE),
LintId::of(&methods::GET_UNWRAP),
LintId::of(&methods::OPTION_EXPECT_USED),
LintId::of(&methods::OPTION_UNWRAP_USED),
LintId::of(&methods::RESULT_EXPECT_USED),
LintId::of(&methods::RESULT_UNWRAP_USED),
LintId::of(&methods::UNWRAP_USED),
LintId::of(&methods::WRONG_PUB_SELF_CONVENTION),
LintId::of(&misc::FLOAT_CMP_CONST),
LintId::of(&misc_early::UNNEEDED_FIELD_PATTERN),

View File

@ -33,40 +33,15 @@ use crate::utils::{
};
declare_clippy_lint! {
/// **What it does:** Checks for `.unwrap()` calls on `Option`s.
/// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s.
///
/// **Why is this bad?** Usually it is better to handle the `None` case, or to
/// at least call `.expect(_)` with a more helpful message. Still, for a lot of
/// **Why is this bad?** It is better to handle the `None` or `Err` case,
/// or at least call `.expect(_)` with a more helpful message. Still, for a lot of
/// quick-and-dirty code, `unwrap` is a good choice, which is why this lint is
/// `Allow` by default.
///
/// **Known problems:** None.
///
/// **Example:**
///
/// Using unwrap on an `Option`:
///
/// ```rust
/// let opt = Some(1);
/// opt.unwrap();
/// ```
///
/// Better:
///
/// ```rust
/// let opt = Some(1);
/// opt.expect("more helpful message");
/// ```
pub OPTION_UNWRAP_USED,
restriction,
"using `Option.unwrap()`, which should at least get a better message using `expect()`"
}
declare_clippy_lint! {
/// **What it does:** Checks for `.unwrap()` calls on `Result`s.
///
/// **Why is this bad?** `result.unwrap()` will let the thread panic on `Err`
/// values. Normally, you want to implement more sophisticated error handling,
/// `result.unwrap()` will let the thread panic on `Err` values.
/// Normally, you want to implement more sophisticated error handling,
/// and propagate errors upwards with `?` operator.
///
/// Even if you want to panic on errors, not all `Error`s implement good
@ -75,23 +50,31 @@ declare_clippy_lint! {
///
/// **Known problems:** None.
///
/// **Example:**
/// Using unwrap on an `Result`:
///
/// **Examples:**
/// ```rust
/// let res: Result<usize, ()> = Ok(1);
/// res.unwrap();
/// # let opt = Some(1);
///
/// // Bad
/// opt.unwrap();
///
/// // Good
/// opt.expect("more helpful message");
/// ```
///
/// Better:
/// // or
///
/// ```rust
/// let res: Result<usize, ()> = Ok(1);
/// # let res: Result<usize, ()> = Ok(1);
///
/// // Bad
/// res.unwrap();
///
/// // Good
/// res.expect("more helpful message");
/// ```
pub RESULT_UNWRAP_USED,
pub UNWRAP_USED,
restriction,
"using `Result.unwrap()`, which might be better handled"
"using `.unwrap()` on `Result` or `Option`, which should at least get a better message using `expect()`"
}
declare_clippy_lint! {
@ -1267,8 +1250,7 @@ declare_clippy_lint! {
}
declare_lint_pass!(Methods => [
OPTION_UNWRAP_USED,
RESULT_UNWRAP_USED,
UNWRAP_USED,
OPTION_EXPECT_USED,
RESULT_EXPECT_USED,
SHOULD_IMPLEMENT_TRAIT,
@ -2397,9 +2379,9 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&unwrap_args[0]));
let mess = if is_type_diagnostic_item(cx, obj_ty, sym!(option_type)) {
Some((OPTION_UNWRAP_USED, "an Option", "None"))
Some((UNWRAP_USED, "an Option", "None"))
} else if is_type_diagnostic_item(cx, obj_ty, sym!(result_type)) {
Some((RESULT_UNWRAP_USED, "a Result", "Err"))
Some((UNWRAP_USED, "a Result", "Err"))
} else {
None
};

View File

@ -1627,13 +1627,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
deprecation: None,
module: "types",
},
Lint {
name: "option_unwrap_used",
group: "restriction",
desc: "using `Option.unwrap()`, which should at least get a better message using `expect()`",
deprecation: None,
module: "methods",
},
Lint {
name: "or_fun_call",
group: "perf",
@ -2404,6 +2397,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
deprecation: None,
module: "returns",
},
Lint {
name: "unwrap_used",
group: "restriction",
desc: "using `.unwrap()` on `Result` or `Option`, which should at least get a better message using `expect()`",
deprecation: None,
module: "methods",
},
Lint {
name: "use_debug",
group: "restriction",

View File

@ -1,4 +1,4 @@
#![warn(clippy::option_unwrap_used, clippy::result_unwrap_used)]
#![warn(clippy::unwrap_used)]
fn unwrap_option() {
let opt = Some(0);

View File

@ -4,7 +4,7 @@ error: used `unwrap()` on `an Option` value
LL | let _ = opt.unwrap();
| ^^^^^^^^^^^^
|
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
= note: `-D clippy::unwrap-used` implied by `-D warnings`
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
error: used `unwrap()` on `a Result` value
@ -13,7 +13,6 @@ error: used `unwrap()` on `a Result` value
LL | let _ = res.unwrap();
| ^^^^^^^^^^^^
|
= note: `-D clippy::result-unwrap-used` implied by `-D warnings`
= help: if you don't want to handle the `Err` case gracefully, consider using `expect()` to provide a better panic message
error: aborting due to 2 previous errors