Skip to content

Commit fecfc4a

Browse files
committed
PR Followups
1 parent 4f5743b commit fecfc4a

File tree

3 files changed

+70
-88
lines changed

3 files changed

+70
-88
lines changed

clippy_lints/src/if_let_some_result.rs

Lines changed: 0 additions & 75 deletions
This file was deleted.

clippy_lints/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ mod future_not_send;
226226
mod get_last_with_len;
227227
mod identity_op;
228228
mod if_let_mutex;
229-
mod if_let_some_result;
230229
mod if_not_else;
231230
mod if_then_some_else_none;
232231
mod implicit_hasher;
@@ -291,6 +290,7 @@ mod needless_borrow;
291290
mod needless_borrowed_ref;
292291
mod needless_continue;
293292
mod needless_for_each;
293+
mod needless_ok;
294294
mod needless_pass_by_value;
295295
mod needless_question_mark;
296296
mod needless_update;
@@ -376,7 +376,6 @@ mod vec;
376376
mod vec_init_then_push;
377377
mod vec_resize_to_zero;
378378
mod verbose_file_reads;
379-
mod while_let_some_result;
380379
mod wildcard_dependencies;
381380
mod wildcard_imports;
382381
mod write;
@@ -656,7 +655,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
656655
get_last_with_len::GET_LAST_WITH_LEN,
657656
identity_op::IDENTITY_OP,
658657
if_let_mutex::IF_LET_MUTEX,
659-
if_let_some_result::IF_LET_SOME_RESULT,
660658
if_not_else::IF_NOT_ELSE,
661659
if_then_some_else_none::IF_THEN_SOME_ELSE_NONE,
662660
implicit_hasher::IMPLICIT_HASHER,
@@ -851,6 +849,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
851849
needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE,
852850
needless_continue::NEEDLESS_CONTINUE,
853851
needless_for_each::NEEDLESS_FOR_EACH,
852+
needless_ok::IF_LET_SOME_RESULT,
853+
needless_ok::WHILE_LET_SOME_RESULT,
854854
needless_pass_by_value::NEEDLESS_PASS_BY_VALUE,
855855
needless_question_mark::NEEDLESS_QUESTION_MARK,
856856
needless_update::NEEDLESS_UPDATE,
@@ -989,7 +989,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
989989
vec_init_then_push::VEC_INIT_THEN_PUSH,
990990
vec_resize_to_zero::VEC_RESIZE_TO_ZERO,
991991
verbose_file_reads::VERBOSE_FILE_READS,
992-
while_let_some_result::WHILE_LET_SOME_RESULT,
993992
wildcard_dependencies::WILDCARD_DEPENDENCIES,
994993
wildcard_imports::ENUM_GLOB_USE,
995994
wildcard_imports::WILDCARD_IMPORTS,
@@ -1250,7 +1249,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12501249
LintId::of(get_last_with_len::GET_LAST_WITH_LEN),
12511250
LintId::of(identity_op::IDENTITY_OP),
12521251
LintId::of(if_let_mutex::IF_LET_MUTEX),
1253-
LintId::of(if_let_some_result::IF_LET_SOME_RESULT),
12541252
LintId::of(indexing_slicing::OUT_OF_BOUNDS_INDEXING),
12551253
LintId::of(infinite_iter::INFINITE_ITER),
12561254
LintId::of(inherent_to_string::INHERENT_TO_STRING),
@@ -1380,6 +1378,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13801378
LintId::of(needless_bool::NEEDLESS_BOOL),
13811379
LintId::of(needless_borrow::NEEDLESS_BORROW),
13821380
LintId::of(needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE),
1381+
LintId::of(needless_ok::IF_LET_SOME_RESULT),
1382+
LintId::of(needless_ok::WHILE_LET_SOME_RESULT),
13831383
LintId::of(needless_question_mark::NEEDLESS_QUESTION_MARK),
13841384
LintId::of(needless_update::NEEDLESS_UPDATE),
13851385
LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),
@@ -1470,7 +1470,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14701470
LintId::of(vec::USELESS_VEC),
14711471
LintId::of(vec_init_then_push::VEC_INIT_THEN_PUSH),
14721472
LintId::of(vec_resize_to_zero::VEC_RESIZE_TO_ZERO),
1473-
LintId::of(while_let_some_result::WHILE_LET_SOME_RESULT),
14741473
LintId::of(write::PRINTLN_EMPTY_STRING),
14751474
LintId::of(write::PRINT_LITERAL),
14761475
LintId::of(write::PRINT_WITH_NEWLINE),
@@ -1505,7 +1504,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15051504
LintId::of(functions::DOUBLE_MUST_USE),
15061505
LintId::of(functions::MUST_USE_UNIT),
15071506
LintId::of(functions::RESULT_UNIT_ERR),
1508-
LintId::of(if_let_some_result::IF_LET_SOME_RESULT),
15091507
LintId::of(inherent_to_string::INHERENT_TO_STRING),
15101508
LintId::of(len_zero::COMPARISON_TO_EMPTY),
15111509
LintId::of(len_zero::LEN_WITHOUT_IS_EMPTY),
@@ -1560,6 +1558,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15601558
LintId::of(mut_mutex_lock::MUT_MUTEX_LOCK),
15611559
LintId::of(mut_reference::UNNECESSARY_MUT_PASSED),
15621560
LintId::of(needless_borrow::NEEDLESS_BORROW),
1561+
LintId::of(needless_ok::IF_LET_SOME_RESULT),
1562+
LintId::of(needless_ok::WHILE_LET_SOME_RESULT),
15631563
LintId::of(neg_multiply::NEG_MULTIPLY),
15641564
LintId::of(new_without_default::NEW_WITHOUT_DEFAULT),
15651565
LintId::of(non_copy_const::BORROW_INTERIOR_MUTABLE_CONST),
@@ -1583,7 +1583,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15831583
LintId::of(unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME),
15841584
LintId::of(unused_unit::UNUSED_UNIT),
15851585
LintId::of(upper_case_acronyms::UPPER_CASE_ACRONYMS),
1586-
LintId::of(while_let_some_result::WHILE_LET_SOME_RESULT),
15871586
LintId::of(write::PRINTLN_EMPTY_STRING),
15881587
LintId::of(write::PRINT_LITERAL),
15891588
LintId::of(write::PRINT_WITH_NEWLINE),
@@ -1975,8 +1974,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
19751974
store.register_late_pass(|| box missing_doc::MissingDoc::new());
19761975
store.register_late_pass(|| box missing_inline::MissingInline);
19771976
store.register_late_pass(move || box exhaustive_items::ExhaustiveItems);
1978-
store.register_late_pass(|| box if_let_some_result::OkIfLet);
1979-
store.register_late_pass(|| box while_let_some_result::WhileLetSomeResult);
1977+
store.register_late_pass(|| box needless_ok::NeedlessOk);
19801978
store.register_late_pass(|| box partialeq_ne_impl::PartialEqNeImpl);
19811979
store.register_late_pass(|| box unused_io_amount::UnusedIoAmount);
19821980
let enum_variant_size_threshold = conf.enum_variant_size_threshold;

clippy_lints/src/while_let_some_result.rs renamed to clippy_lints/src/needless_ok.rs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,36 @@ use rustc_lint::{LateContext, LateLintPass};
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
1010
use rustc_span::sym;
1111

12+
declare_clippy_lint! {
13+
/// ### What it does
14+
///* Checks for unnecessary `ok()` in if let.
15+
///
16+
/// ### Why is this bad?
17+
/// Calling `ok()` in if let is unnecessary, instead match
18+
/// on `Ok(pat)`
19+
///
20+
/// ### Example
21+
/// ```ignore
22+
/// for i in iter {
23+
/// if let Some(value) = i.parse().ok() {
24+
/// vec.push(value)
25+
/// }
26+
/// }
27+
/// ```
28+
/// Could be written:
29+
///
30+
/// ```ignore
31+
/// for i in iter {
32+
/// if let Ok(value) = i.parse() {
33+
/// vec.push(value)
34+
/// }
35+
/// }
36+
/// ```
37+
pub IF_LET_SOME_RESULT,
38+
style,
39+
"usage of `ok()` in `if let Some(pat)` statements is unnecessary, match on `Ok(pat)` instead"
40+
}
41+
1242
declare_clippy_lint! {
1343
/// ### What it does
1444
/// Checks for unnecessary `ok()` in `while let`.
@@ -34,11 +64,40 @@ declare_clippy_lint! {
3464
"usage of `ok()` in `while let Some(pat)` statements is unnecessary, match on `Ok(pat)` instead"
3565
}
3666

37-
declare_lint_pass!(WhileLetSomeResult => [WHILE_LET_SOME_RESULT]);
67+
declare_lint_pass!(NeedlessOk => [IF_LET_SOME_RESULT, WHILE_LET_SOME_RESULT]);
3868

39-
impl<'tcx> LateLintPass<'tcx> for WhileLetSomeResult {
69+
impl<'tcx> LateLintPass<'tcx> for NeedlessOk {
4070
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
41-
if_chain! {
71+
if_chain! { // IF_LET_SOME_RESULT check
72+
if let ExprKind::Match(op, body, MatchSource::IfLetDesugar { .. }) = expr.kind; // test if expr is if let
73+
if let ExprKind::MethodCall(_, ok_span, result_types, _) = op.kind;
74+
if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = body[0].pat.kind; // get operation
75+
if method_chain_args(op, &["ok"]).is_some(); // test to see if using ok() methoduse std::marker::Sized;
76+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&result_types[0]), sym::result_type);
77+
if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some";
78+
79+
then {
80+
let mut applicability = Applicability::MachineApplicable;
81+
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
82+
let trimmed_ok = snippet_with_applicability(cx, op.span.until(ok_span), "", &mut applicability);
83+
let sugg = format!(
84+
"if let Ok({}) = {}",
85+
some_expr_string,
86+
trimmed_ok.trim().trim_end_matches('.'),
87+
);
88+
span_lint_and_sugg(
89+
cx,
90+
IF_LET_SOME_RESULT,
91+
expr.span.with_hi(op.span.hi()),
92+
"matching on `Some` with `ok()` is redundant",
93+
&format!("consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
94+
sugg,
95+
applicability,
96+
);
97+
}
98+
}
99+
100+
if_chain! { // WHILE_LET_SOME_RESULT check
42101
if let ExprKind::Match(op, body, MatchSource::WhileLetDesugar { .. }) = expr.kind; // test if expr is `while let`
43102
if let ExprKind::MethodCall(_, ok_span, result_types, _) = op.kind;
44103
if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = body[0].pat.kind; // get operation

0 commit comments

Comments
 (0)