Skip to content

Commit 0e8be59

Browse files
committed
Merge option_expect_used and result_expect_used lints into expect_used lint
1 parent bcf6166 commit 0e8be59

File tree

6 files changed

+35
-63
lines changed

6 files changed

+35
-63
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,7 @@ Released 2018-09-13
13371337
[`excessive_precision`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_precision
13381338
[`exit`]: https://rust-lang.github.io/rust-clippy/master/index.html#exit
13391339
[`expect_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
1340+
[`expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#expect_used
13401341
[`expl_impl_clone_on_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#expl_impl_clone_on_copy
13411342
[`explicit_counter_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_counter_loop
13421343
[`explicit_deref_methods`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_deref_methods
@@ -1497,7 +1498,6 @@ Released 2018-09-13
14971498
[`option_and_then_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_and_then_some
14981499
[`option_as_ref_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_as_ref_deref
14991500
[`option_env_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_env_unwrap
1500-
[`option_expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_expect_used
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
@@ -1537,7 +1537,6 @@ Released 2018-09-13
15371537
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro
15381538
[`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts
15391539
[`rest_pat_in_fully_bound_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#rest_pat_in_fully_bound_structs
1540-
[`result_expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_expect_used
15411540
[`result_map_or_into_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_or_into_option
15421541
[`result_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_unit_fn
15431542
[`result_unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_unwrap_used

clippy_lints/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
657657
&methods::CLONE_ON_COPY,
658658
&methods::CLONE_ON_REF_PTR,
659659
&methods::EXPECT_FUN_CALL,
660+
&methods::EXPECT_USED,
660661
&methods::FILETYPE_IS_FILE,
661662
&methods::FILTER_MAP,
662663
&methods::FILTER_MAP_NEXT,
@@ -678,10 +679,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
678679
&methods::OK_EXPECT,
679680
&methods::OPTION_AND_THEN_SOME,
680681
&methods::OPTION_AS_REF_DEREF,
681-
&methods::OPTION_EXPECT_USED,
682682
&methods::OPTION_MAP_OR_NONE,
683683
&methods::OR_FUN_CALL,
684-
&methods::RESULT_EXPECT_USED,
685684
&methods::RESULT_MAP_OR_INTO_OPTION,
686685
&methods::SEARCH_IS_SOME,
687686
&methods::SHOULD_IMPLEMENT_TRAIT,
@@ -1086,10 +1085,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10861085
LintId::of(&matches::WILDCARD_ENUM_MATCH_ARM),
10871086
LintId::of(&mem_forget::MEM_FORGET),
10881087
LintId::of(&methods::CLONE_ON_REF_PTR),
1088+
LintId::of(&methods::EXPECT_USED),
10891089
LintId::of(&methods::FILETYPE_IS_FILE),
10901090
LintId::of(&methods::GET_UNWRAP),
1091-
LintId::of(&methods::OPTION_EXPECT_USED),
1092-
LintId::of(&methods::RESULT_EXPECT_USED),
10931091
LintId::of(&methods::UNWRAP_USED),
10941092
LintId::of(&methods::WRONG_PUB_SELF_CONVENTION),
10951093
LintId::of(&misc::FLOAT_CMP_CONST),

clippy_lints/src/methods/mod.rs

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,61 +78,45 @@ declare_clippy_lint! {
7878
}
7979

8080
declare_clippy_lint! {
81-
/// **What it does:** Checks for `.expect()` calls on `Option`s.
81+
/// **What it does:** Checks for `.expect()` calls on `Option`s and `Result`s.
8282
///
83-
/// **Why is this bad?** Usually it is better to handle the `None` case. Still,
84-
/// for a lot of quick-and-dirty code, `expect` is a good choice, which is why
85-
/// this lint is `Allow` by default.
83+
/// **Why is this bad?** Usually it is better to handle the `None` or `Err` case.
84+
/// Still, for a lot of quick-and-dirty code, `expect` is a good choice, which is why
85+
/// this lint is `Allow` by default.
8686
///
87-
/// **Known problems:** None.
87+
/// `result.expect()` will let the thread panic on `Err`
88+
/// values. Normally, you want to implement more sophisticated error handling,
89+
/// and propagate errors upwards with `?` operator.
8890
///
89-
/// **Example:**
91+
/// **Known problems:** None.
9092
///
91-
/// Using expect on an `Option`:
93+
/// **Examples:**
94+
/// ```rust,ignore
95+
/// # let opt = Some(1);
9296
///
93-
/// ```rust
94-
/// let opt = Some(1);
97+
/// // Bad
9598
/// opt.expect("one");
96-
/// ```
97-
///
98-
/// Better:
9999
///
100-
/// ```rust,ignore
100+
/// // Good
101101
/// let opt = Some(1);
102102
/// opt?;
103103
/// ```
104-
pub OPTION_EXPECT_USED,
105-
restriction,
106-
"using `Option.expect()`, which might be better handled"
107-
}
108-
109-
declare_clippy_lint! {
110-
/// **What it does:** Checks for `.expect()` calls on `Result`s.
111-
///
112-
/// **Why is this bad?** `result.expect()` will let the thread panic on `Err`
113-
/// values. Normally, you want to implement more sophisticated error handling,
114-
/// and propagate errors upwards with `?` operator.
115-
///
116-
/// **Known problems:** None.
117104
///
118-
/// **Example:**
119-
/// Using expect on an `Result`:
105+
/// // or
120106
///
121107
/// ```rust
122-
/// let res: Result<usize, ()> = Ok(1);
123-
/// res.expect("one");
124-
/// ```
108+
/// # let res: Result<usize, ()> = Ok(1);
125109
///
126-
/// Better:
110+
/// // Bad
111+
/// res.expect("one");
127112
///
128-
/// ```rust
129-
/// let res: Result<usize, ()> = Ok(1);
113+
/// // Good
130114
/// res?;
131115
/// # Ok::<(), ()>(())
132116
/// ```
133-
pub RESULT_EXPECT_USED,
117+
pub EXPECT_USED,
134118
restriction,
135-
"using `Result.expect()`, which might be better handled"
119+
"using `.expect()` on `Result` or `Option`, which might be better handled"
136120
}
137121

138122
declare_clippy_lint! {
@@ -1251,8 +1235,7 @@ declare_clippy_lint! {
12511235

12521236
declare_lint_pass!(Methods => [
12531237
UNWRAP_USED,
1254-
OPTION_EXPECT_USED,
1255-
RESULT_EXPECT_USED,
1238+
EXPECT_USED,
12561239
SHOULD_IMPLEMENT_TRAIT,
12571240
WRONG_SELF_CONVENTION,
12581241
WRONG_PUB_SELF_CONVENTION,
@@ -2407,9 +2390,9 @@ fn lint_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, expect_args: &[hi
24072390
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&expect_args[0]));
24082391

24092392
let mess = if is_type_diagnostic_item(cx, obj_ty, sym!(option_type)) {
2410-
Some((OPTION_EXPECT_USED, "an Option", "None"))
2393+
Some((EXPECT_USED, "an Option", "None"))
24112394
} else if is_type_diagnostic_item(cx, obj_ty, sym!(result_type)) {
2412-
Some((RESULT_EXPECT_USED, "a Result", "Err"))
2395+
Some((EXPECT_USED, "a Result", "Err"))
24132396
} else {
24142397
None
24152398
};

src/lintlist/mod.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
514514
deprecation: None,
515515
module: "methods",
516516
},
517+
Lint {
518+
name: "expect_used",
519+
group: "restriction",
520+
desc: "using `.expect()` on `Result` or `Option`, which might be better handled",
521+
deprecation: None,
522+
module: "methods",
523+
},
517524
Lint {
518525
name: "expl_impl_clone_on_copy",
519526
group: "pedantic",
@@ -1599,13 +1606,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
15991606
deprecation: None,
16001607
module: "option_env_unwrap",
16011608
},
1602-
Lint {
1603-
name: "option_expect_used",
1604-
group: "restriction",
1605-
desc: "using `Option.expect()`, which might be better handled",
1606-
deprecation: None,
1607-
module: "methods",
1608-
},
16091609
Lint {
16101610
name: "option_map_or_none",
16111611
group: "style",
@@ -1865,13 +1865,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
18651865
deprecation: None,
18661866
module: "matches",
18671867
},
1868-
Lint {
1869-
name: "result_expect_used",
1870-
group: "restriction",
1871-
desc: "using `Result.expect()`, which might be better handled",
1872-
deprecation: None,
1873-
module: "methods",
1874-
},
18751868
Lint {
18761869
name: "result_map_or_into_option",
18771870
group: "style",

tests/ui/expect.rs

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

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

tests/ui/expect.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: used `expect()` on `an Option` value
44
LL | let _ = opt.expect("");
55
| ^^^^^^^^^^^^^^
66
|
7-
= note: `-D clippy::option-expect-used` implied by `-D warnings`
7+
= note: `-D clippy::expect-used` implied by `-D warnings`
88
= help: if this value is an `None`, it will panic
99

1010
error: used `expect()` on `a Result` value
@@ -13,7 +13,6 @@ error: used `expect()` on `a Result` value
1313
LL | let _ = res.expect("");
1414
| ^^^^^^^^^^^^^^
1515
|
16-
= note: `-D clippy::result-expect-used` implied by `-D warnings`
1716
= help: if this value is an `Err`, it will panic
1817

1918
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)