Skip to content

Commit bcf6166

Browse files
committed
Merge option_unwrap_used and result_unwrap_used lints into unwrap_used lint
1 parent 6cbdd1e commit bcf6166

File tree

6 files changed

+37
-58
lines changed

6 files changed

+37
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,6 @@ Released 2018-09-13
15011501
[`option_map_or_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_or_none
15021502
[`option_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn
15031503
[`option_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_option
1504-
[`option_unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_unwrap_used
15051504
[`or_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
15061505
[`out_of_bounds_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#out_of_bounds_indexing
15071506
[`overflow_check_conditional`]: https://rust-lang.github.io/rust-clippy/master/index.html#overflow_check_conditional
@@ -1622,6 +1621,7 @@ Released 2018-09-13
16221621
[`unused_label`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_label
16231622
[`unused_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_self
16241623
[`unused_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit
1624+
[`unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
16251625
[`use_debug`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_debug
16261626
[`use_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#use_self
16271627
[`used_underscore_binding`]: https://rust-lang.github.io/rust-clippy/master/index.html#used_underscore_binding

clippy_lints/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
680680
&methods::OPTION_AS_REF_DEREF,
681681
&methods::OPTION_EXPECT_USED,
682682
&methods::OPTION_MAP_OR_NONE,
683-
&methods::OPTION_UNWRAP_USED,
684683
&methods::OR_FUN_CALL,
685684
&methods::RESULT_EXPECT_USED,
686685
&methods::RESULT_MAP_OR_INTO_OPTION,
687-
&methods::RESULT_UNWRAP_USED,
688686
&methods::SEARCH_IS_SOME,
689687
&methods::SHOULD_IMPLEMENT_TRAIT,
690688
&methods::SINGLE_CHAR_PATTERN,
@@ -695,6 +693,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
695693
&methods::UNINIT_ASSUMED_INIT,
696694
&methods::UNNECESSARY_FILTER_MAP,
697695
&methods::UNNECESSARY_FOLD,
696+
&methods::UNWRAP_USED,
698697
&methods::USELESS_ASREF,
699698
&methods::WRONG_PUB_SELF_CONVENTION,
700699
&methods::WRONG_SELF_CONVENTION,
@@ -1090,9 +1089,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10901089
LintId::of(&methods::FILETYPE_IS_FILE),
10911090
LintId::of(&methods::GET_UNWRAP),
10921091
LintId::of(&methods::OPTION_EXPECT_USED),
1093-
LintId::of(&methods::OPTION_UNWRAP_USED),
10941092
LintId::of(&methods::RESULT_EXPECT_USED),
1095-
LintId::of(&methods::RESULT_UNWRAP_USED),
1093+
LintId::of(&methods::UNWRAP_USED),
10961094
LintId::of(&methods::WRONG_PUB_SELF_CONVENTION),
10971095
LintId::of(&misc::FLOAT_CMP_CONST),
10981096
LintId::of(&misc_early::UNNEEDED_FIELD_PATTERN),

clippy_lints/src/methods/mod.rs

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,15 @@ use crate::utils::{
3333
};
3434

3535
declare_clippy_lint! {
36-
/// **What it does:** Checks for `.unwrap()` calls on `Option`s.
36+
/// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s.
3737
///
38-
/// **Why is this bad?** Usually it is better to handle the `None` case, or to
39-
/// at least call `.expect(_)` with a more helpful message. Still, for a lot of
38+
/// **Why is this bad?** It is better to handle the `None` or `Err` case,
39+
/// or at least call `.expect(_)` with a more helpful message. Still, for a lot of
4040
/// quick-and-dirty code, `unwrap` is a good choice, which is why this lint is
4141
/// `Allow` by default.
4242
///
43-
/// **Known problems:** None.
44-
///
45-
/// **Example:**
46-
///
47-
/// Using unwrap on an `Option`:
48-
///
49-
/// ```rust
50-
/// let opt = Some(1);
51-
/// opt.unwrap();
52-
/// ```
53-
///
54-
/// Better:
55-
///
56-
/// ```rust
57-
/// let opt = Some(1);
58-
/// opt.expect("more helpful message");
59-
/// ```
60-
pub OPTION_UNWRAP_USED,
61-
restriction,
62-
"using `Option.unwrap()`, which should at least get a better message using `expect()`"
63-
}
64-
65-
declare_clippy_lint! {
66-
/// **What it does:** Checks for `.unwrap()` calls on `Result`s.
67-
///
68-
/// **Why is this bad?** `result.unwrap()` will let the thread panic on `Err`
69-
/// values. Normally, you want to implement more sophisticated error handling,
43+
/// `result.unwrap()` will let the thread panic on `Err` values.
44+
/// Normally, you want to implement more sophisticated error handling,
7045
/// and propagate errors upwards with `?` operator.
7146
///
7247
/// Even if you want to panic on errors, not all `Error`s implement good
@@ -75,23 +50,31 @@ declare_clippy_lint! {
7550
///
7651
/// **Known problems:** None.
7752
///
78-
/// **Example:**
79-
/// Using unwrap on an `Result`:
80-
///
53+
/// **Examples:**
8154
/// ```rust
82-
/// let res: Result<usize, ()> = Ok(1);
83-
/// res.unwrap();
55+
/// # let opt = Some(1);
56+
///
57+
/// // Bad
58+
/// opt.unwrap();
59+
///
60+
/// // Good
61+
/// opt.expect("more helpful message");
8462
/// ```
8563
///
86-
/// Better:
64+
/// // or
8765
///
8866
/// ```rust
89-
/// let res: Result<usize, ()> = Ok(1);
67+
/// # let res: Result<usize, ()> = Ok(1);
68+
///
69+
/// // Bad
70+
/// res.unwrap();
71+
///
72+
/// // Good
9073
/// res.expect("more helpful message");
9174
/// ```
92-
pub RESULT_UNWRAP_USED,
75+
pub UNWRAP_USED,
9376
restriction,
94-
"using `Result.unwrap()`, which might be better handled"
77+
"using `.unwrap()` on `Result` or `Option`, which should at least get a better message using `expect()`"
9578
}
9679

9780
declare_clippy_lint! {
@@ -1267,8 +1250,7 @@ declare_clippy_lint! {
12671250
}
12681251

12691252
declare_lint_pass!(Methods => [
1270-
OPTION_UNWRAP_USED,
1271-
RESULT_UNWRAP_USED,
1253+
UNWRAP_USED,
12721254
OPTION_EXPECT_USED,
12731255
RESULT_EXPECT_USED,
12741256
SHOULD_IMPLEMENT_TRAIT,
@@ -2397,9 +2379,9 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
23972379
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&unwrap_args[0]));
23982380

23992381
let mess = if is_type_diagnostic_item(cx, obj_ty, sym!(option_type)) {
2400-
Some((OPTION_UNWRAP_USED, "an Option", "None"))
2382+
Some((UNWRAP_USED, "an Option", "None"))
24012383
} else if is_type_diagnostic_item(cx, obj_ty, sym!(result_type)) {
2402-
Some((RESULT_UNWRAP_USED, "a Result", "Err"))
2384+
Some((UNWRAP_USED, "a Result", "Err"))
24032385
} else {
24042386
None
24052387
};

src/lintlist/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,13 +1627,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
16271627
deprecation: None,
16281628
module: "types",
16291629
},
1630-
Lint {
1631-
name: "option_unwrap_used",
1632-
group: "restriction",
1633-
desc: "using `Option.unwrap()`, which should at least get a better message using `expect()`",
1634-
deprecation: None,
1635-
module: "methods",
1636-
},
16371630
Lint {
16381631
name: "or_fun_call",
16391632
group: "perf",
@@ -2404,6 +2397,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
24042397
deprecation: None,
24052398
module: "returns",
24062399
},
2400+
Lint {
2401+
name: "unwrap_used",
2402+
group: "restriction",
2403+
desc: "using `.unwrap()` on `Result` or `Option`, which should at least get a better message using `expect()`",
2404+
deprecation: None,
2405+
module: "methods",
2406+
},
24072407
Lint {
24082408
name: "use_debug",
24092409
group: "restriction",

tests/ui/unwrap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::option_unwrap_used, clippy::result_unwrap_used)]
1+
#![warn(clippy::unwrap_used)]
22

33
fn unwrap_option() {
44
let opt = Some(0);

tests/ui/unwrap.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: used `unwrap()` on `an Option` value
44
LL | let _ = opt.unwrap();
55
| ^^^^^^^^^^^^
66
|
7-
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
7+
= note: `-D clippy::unwrap-used` implied by `-D warnings`
88
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
99

1010
error: used `unwrap()` on `a Result` value
@@ -13,7 +13,6 @@ error: used `unwrap()` on `a Result` value
1313
LL | let _ = res.unwrap();
1414
| ^^^^^^^^^^^^
1515
|
16-
= note: `-D clippy::result-unwrap-used` implied by `-D warnings`
1716
= help: if you don't want to handle the `Err` case gracefully, consider using `expect()` to provide a better panic message
1817

1918
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)