Auto merge of #5563 - ThibsG:MergeLints, r=flip1995

Merge some lints together

This PR merges following lints:

- `block_in_if_condition_expr` and `block_in_if_condition_stmt` → `blocks_in_if_conditions`
- `option_map_unwrap_or`, `option_map_unwrap_or_else` and `result_map_unwrap_or_else` → `map_unwrap`
- `option_unwrap_used` and `result_unwrap_used` → `unwrap_used`
- `option_expect_used` and `result_expect_used` → `expect_used`
- `wrong_pub_self_convention` into `wrong_self_convention`
- `for_loop_over_option` and `for_loop_over_result` → `for_loops_over_fallibles`

Lints that have already been merged since the issue was created:
- [x] `new_without_default` and `new_without_default_derive` → `new_without_default`

Need more discussion:
- `string_add` and `string_add_assign`: do we agree to merge them or not? Is there something more to do? → **not merge finally**
- `identity_op` and `modulo_one` → `useless_arithmetic`: seems outdated, since `modulo_arithmetic` has been created.

fixes #1078

changelog: Merging some lints together:
- `block_in_if_condition_expr` and `block_in_if_condition_stmt` → `blocks_in_if_conditions`
- `option_map_unwrap_or`, `option_map_unwrap_or_else` and `result_map_unwrap_or_else` → `map_unwrap_or`
- `option_unwrap_used` and `result_unwrap_used` → `unwrap_used`
- `option_expect_used` and `result_expect_used` → `expect_used`
- `for_loop_over_option` and `for_loop_over_result` → `for_loops_over_fallibles`
This commit is contained in:
bors 2020-05-16 20:17:11 +00:00
commit cfd720d506
22 changed files with 288 additions and 449 deletions

View File

@ -198,7 +198,7 @@ Released 2020-03-12
### Suggestion Improvements
* [`option_map_unwrap_or`] [#4634](https://github.com/rust-lang/rust-clippy/pull/4634)
* `option_map_unwrap_or` [#4634](https://github.com/rust-lang/rust-clippy/pull/4634)
* [`wildcard_enum_match_arm`] [#4934](https://github.com/rust-lang/rust-clippy/pull/4934)
* [`cognitive_complexity`] [#4935](https://github.com/rust-lang/rust-clippy/pull/4935)
* [`decimal_literal_representation`] [#4956](https://github.com/rust-lang/rust-clippy/pull/4956)
@ -282,8 +282,8 @@ Released 2019-12-19
* [`panic`] [#4657](https://github.com/rust-lang/rust-clippy/pull/4657)
* [`unreachable`] [#4657](https://github.com/rust-lang/rust-clippy/pull/4657)
* [`todo`] [#4657](https://github.com/rust-lang/rust-clippy/pull/4657)
* [`option_expect_used`] [#4657](https://github.com/rust-lang/rust-clippy/pull/4657)
* [`result_expect_used`] [#4657](https://github.com/rust-lang/rust-clippy/pull/4657)
* `option_expect_used` [#4657](https://github.com/rust-lang/rust-clippy/pull/4657)
* `result_expect_used` [#4657](https://github.com/rust-lang/rust-clippy/pull/4657)
* Move `redundant_clone` to perf group [#4509](https://github.com/rust-lang/rust-clippy/pull/4509)
* Move `manual_mul_add` to nursery group [#4736](https://github.com/rust-lang/rust-clippy/pull/4736)
* Expand `unit_cmp` to also work with `assert_eq!`, `debug_assert_eq!`, `assert_ne!` and `debug_assert_ne!` [#4613](https://github.com/rust-lang/rust-clippy/pull/4613)
@ -395,7 +395,7 @@ Released 2019-08-15
* Fix false positive in [`useless_attribute`] [#4107](https://github.com/rust-lang/rust-clippy/pull/4107)
* Fix incorrect suggestion for [`float_cmp`] [#4214](https://github.com/rust-lang/rust-clippy/pull/4214)
* Add suggestions for [`print_with_newline`] and [`write_with_newline`] [#4136](https://github.com/rust-lang/rust-clippy/pull/4136)
* Improve suggestions for [`option_map_unwrap_or_else`] and [`result_map_unwrap_or_else`] [#4164](https://github.com/rust-lang/rust-clippy/pull/4164)
* Improve suggestions for `option_map_unwrap_or_else` and `result_map_unwrap_or_else` [#4164](https://github.com/rust-lang/rust-clippy/pull/4164)
* Improve suggestions for [`non_ascii_literal`] [#4119](https://github.com/rust-lang/rust-clippy/pull/4119)
* Improve diagnostics for [`let_and_return`] [#4137](https://github.com/rust-lang/rust-clippy/pull/4137)
* Improve diagnostics for [`trivially_copy_pass_by_ref`] [#4071](https://github.com/rust-lang/rust-clippy/pull/4071)
@ -448,7 +448,7 @@ Released 2019-05-20
* Fix false positive in [`needless_range_loop`] pertaining to structs without a `.iter()`
* Fix false positive in [`bool_comparison`] pertaining to non-bool types
* Fix false positive in [`redundant_closure`] pertaining to differences in borrows
* Fix false positive in [`option_map_unwrap_or`] on non-copy types
* Fix false positive in `option_map_unwrap_or` on non-copy types
* Fix false positives in [`missing_const_for_fn`] pertaining to macros and trait method impls
* Fix false positive in [`needless_pass_by_value`] pertaining to procedural macros
* Fix false positive in [`needless_continue`] pertaining to loop labels
@ -794,7 +794,7 @@ Released 2018-09-13
## 0.0.169
* Rustup to *rustc 1.23.0-nightly (3b82e4c74 2017-11-05)*
* New lints: [`just_underscores_and_digits`], [`result_map_unwrap_or_else`], [`transmute_bytes_to_str`]
* New lints: [`just_underscores_and_digits`], `result_map_unwrap_or_else`, [`transmute_bytes_to_str`]
## 0.0.168
* Rustup to *rustc 1.23.0-nightly (f0fe716db 2017-10-30)*
@ -1068,7 +1068,7 @@ Released 2018-09-13
## 0.0.93 — 2016-10-03
* Rustup to *rustc 1.14.0-nightly (144af3e97 2016-10-02)*
* [`option_map_unwrap_or`] and [`option_map_unwrap_or_else`] are now
* `option_map_unwrap_or` and `option_map_unwrap_or_else` are now
allowed by default.
* New lint: [`explicit_into_iter_loop`]
@ -1087,8 +1087,8 @@ Released 2018-09-13
## 0.0.88 — 2016-09-04
* Rustup to *rustc 1.13.0-nightly (70598e04f 2016-09-03)*
* The following lints are not new but were only usable through the `clippy`
lint groups: [`filter_next`], [`for_loop_over_option`],
[`for_loop_over_result`] and [`match_overlapping_arm`]. You should now be
lint groups: [`filter_next`], `for_loop_over_option`,
`for_loop_over_result` and [`match_overlapping_arm`]. You should now be
able to `#[allow/deny]` them individually and they are available directly
through `cargo clippy`.
@ -1274,8 +1274,7 @@ Released 2018-09-13
[`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_lock
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask
[`blacklisted_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name
[`block_in_if_condition_expr`]: https://rust-lang.github.io/rust-clippy/master/index.html#block_in_if_condition_expr
[`block_in_if_condition_stmt`]: https://rust-lang.github.io/rust-clippy/master/index.html#block_in_if_condition_stmt
[`blocks_in_if_conditions`]: https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_if_conditions
[`bool_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
[`borrow_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const
[`borrowed_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box
@ -1338,6 +1337,7 @@ Released 2018-09-13
[`excessive_precision`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_precision
[`exit`]: https://rust-lang.github.io/rust-clippy/master/index.html#exit
[`expect_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
[`expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#expect_used
[`expl_impl_clone_on_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#expl_impl_clone_on_copy
[`explicit_counter_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_counter_loop
[`explicit_deref_methods`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_deref_methods
@ -1361,8 +1361,7 @@ Released 2018-09-13
[`fn_to_numeric_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast
[`fn_to_numeric_cast_with_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast_with_truncation
[`for_kv_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_kv_map
[`for_loop_over_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_loop_over_option
[`for_loop_over_result`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_loop_over_result
[`for_loops_over_fallibles`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_loops_over_fallibles
[`forget_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#forget_copy
[`forget_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#forget_ref
[`future_not_send`]: https://rust-lang.github.io/rust-clippy/master/index.html#future_not_send
@ -1431,6 +1430,7 @@ Released 2018-09-13
[`map_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
[`map_entry`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_entry
[`map_flatten`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten
[`map_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or
[`match_as_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_as_ref
[`match_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_bool
[`match_on_vec_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_on_vec_items
@ -1497,13 +1497,9 @@ Released 2018-09-13
[`option_and_then_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_and_then_some
[`option_as_ref_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_as_ref_deref
[`option_env_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_env_unwrap
[`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
[`option_map_unwrap_or_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unwrap_or_else
[`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
@ -1540,11 +1536,8 @@ Released 2018-09-13
[`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
[`rest_pat_in_fully_bound_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#rest_pat_in_fully_bound_structs
[`result_expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_expect_used
[`result_map_or_into_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_or_into_option
[`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
[`reversed_empty_ranges`]: https://rust-lang.github.io/rust-clippy/master/index.html#reversed_empty_ranges
[`same_functions_in_if_condition`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_functions_in_if_condition
[`search_is_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#search_is_some
@ -1625,6 +1618,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

@ -8,43 +8,40 @@ use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! {
/// **What it does:** Checks for `if` conditions that use blocks to contain an
/// expression.
/// **What it does:** Checks for `if` conditions that use blocks containing an
/// expression, statements or conditions that use closures with blocks.
///
/// **Why is this bad?** It isn't really Rust style, same as using parentheses
/// to contain expressions.
/// **Why is this bad?** Style, using blocks in the condition makes it hard to read.
///
/// **Known problems:** None.
///
/// **Example:**
/// **Examples:**
/// ```rust
/// // Bad
/// if { true } { /* ... */ }
///
/// // Good
/// if true { /* ... */ }
/// ```
pub BLOCK_IN_IF_CONDITION_EXPR,
style,
"braces that can be eliminated in conditions, e.g., `if { true } ...`"
}
declare_clippy_lint! {
/// **What it does:** Checks for `if` conditions that use blocks containing
/// statements, or conditions that use closures with blocks.
///
/// **Why is this bad?** Using blocks in the condition makes it hard to read.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust,ignore
/// if { let x = somefunc(); x } {}
/// // or
/// if somefunc(|x| { x == 47 }) {}
///
/// ```rust
/// # fn somefunc() -> bool { true };
///
/// // Bad
/// if { let x = somefunc(); x } { /* ... */ }
///
/// // Good
/// let res = { let x = somefunc(); x };
/// if res { /* ... */ }
/// ```
pub BLOCK_IN_IF_CONDITION_STMT,
pub BLOCKS_IN_IF_CONDITIONS,
style,
"complex blocks in conditions, e.g., `if { let x = true; x } ...`"
"useless or complex blocks that can be eliminated in conditions"
}
declare_lint_pass!(BlockInIfCondition => [BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT]);
declare_lint_pass!(BlocksInIfConditions => [BLOCKS_IN_IF_CONDITIONS]);
struct ExVisitor<'a, 'tcx> {
found_block: Option<&'tcx Expr<'tcx>>,
@ -72,9 +69,9 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
const COMPLEX_BLOCK_MESSAGE: &str = "in an `if` condition, avoid complex blocks or closures with blocks; \
instead, move the block or closure higher and bind it with a `let`";
instead, move the block or closure higher and bind it with a `let`";
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlocksInIfConditions {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
if in_external_macro(cx.sess(), expr.span) {
return;
@ -92,7 +89,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
BLOCK_IN_IF_CONDITION_EXPR,
BLOCKS_IN_IF_CONDITIONS,
cond.span,
BRACED_EXPR_MESSAGE,
"try",
@ -118,7 +115,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
BLOCK_IN_IF_CONDITION_STMT,
BLOCKS_IN_IF_CONDITIONS,
expr.span.with_hi(cond.span.hi()),
COMPLEX_BLOCK_MESSAGE,
"try",
@ -140,7 +137,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
let mut visitor = ExVisitor { found_block: None, cx };
walk_expr(&mut visitor, cond);
if let Some(block) = visitor.found_block {
span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);
span_lint(cx, BLOCKS_IN_IF_CONDITIONS, block.span, COMPLEX_BLOCK_MESSAGE);
}
}
}

View File

@ -180,7 +180,7 @@ mod attrs;
mod await_holding_lock;
mod bit_mask;
mod blacklisted_name;
mod block_in_if_condition;
mod blocks_in_if_conditions;
mod booleans;
mod bytecount;
mod cargo_common_metadata;
@ -507,8 +507,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&bit_mask::INEFFECTIVE_BIT_MASK,
&bit_mask::VERBOSE_BIT_MASK,
&blacklisted_name::BLACKLISTED_NAME,
&block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR,
&block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,
&blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS,
&booleans::LOGIC_BUG,
&booleans::NONMINIMAL_BOOL,
&bytecount::NAIVE_BYTECOUNT,
@ -616,8 +615,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&loops::EXPLICIT_INTO_ITER_LOOP,
&loops::EXPLICIT_ITER_LOOP,
&loops::FOR_KV_MAP,
&loops::FOR_LOOP_OVER_OPTION,
&loops::FOR_LOOP_OVER_RESULT,
&loops::FOR_LOOPS_OVER_FALLIBLES,
&loops::ITER_NEXT_LOOP,
&loops::MANUAL_MEMCPY,
&loops::MUT_RANGE_BOUND,
@ -658,6 +656,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&methods::CLONE_ON_COPY,
&methods::CLONE_ON_REF_PTR,
&methods::EXPECT_FUN_CALL,
&methods::EXPECT_USED,
&methods::FILETYPE_IS_FILE,
&methods::FILTER_MAP,
&methods::FILTER_MAP_NEXT,
@ -674,20 +673,14 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&methods::ITER_SKIP_NEXT,
&methods::MANUAL_SATURATING_ARITHMETIC,
&methods::MAP_FLATTEN,
&methods::MAP_UNWRAP_OR,
&methods::NEW_RET_NO_SELF,
&methods::OK_EXPECT,
&methods::OPTION_AND_THEN_SOME,
&methods::OPTION_AS_REF_DEREF,
&methods::OPTION_EXPECT_USED,
&methods::OPTION_MAP_OR_NONE,
&methods::OPTION_MAP_UNWRAP_OR,
&methods::OPTION_MAP_UNWRAP_OR_ELSE,
&methods::OPTION_UNWRAP_USED,
&methods::OR_FUN_CALL,
&methods::RESULT_EXPECT_USED,
&methods::RESULT_MAP_OR_INTO_OPTION,
&methods::RESULT_MAP_UNWRAP_OR_ELSE,
&methods::RESULT_UNWRAP_USED,
&methods::SEARCH_IS_SOME,
&methods::SHOULD_IMPLEMENT_TRAIT,
&methods::SINGLE_CHAR_PATTERN,
@ -698,6 +691,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,
@ -900,7 +894,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box mut_reference::UnnecessaryMutPassed);
store.register_late_pass(|| box len_zero::LenZero);
store.register_late_pass(|| box attrs::Attributes);
store.register_late_pass(|| box block_in_if_condition::BlockInIfCondition);
store.register_late_pass(|| box blocks_in_if_conditions::BlocksInIfConditions);
store.register_late_pass(|| box unicode::Unicode);
store.register_late_pass(|| box strings::StringAdd);
store.register_late_pass(|| box implicit_return::ImplicitReturn);
@ -1090,12 +1084,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&matches::WILDCARD_ENUM_MATCH_ARM),
LintId::of(&mem_forget::MEM_FORGET),
LintId::of(&methods::CLONE_ON_REF_PTR),
LintId::of(&methods::EXPECT_USED),
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),
@ -1153,9 +1145,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&methods::FIND_MAP),
LintId::of(&methods::INEFFICIENT_TO_STRING),
LintId::of(&methods::MAP_FLATTEN),
LintId::of(&methods::OPTION_MAP_UNWRAP_OR),
LintId::of(&methods::OPTION_MAP_UNWRAP_OR_ELSE),
LintId::of(&methods::RESULT_MAP_UNWRAP_OR_ELSE),
LintId::of(&methods::MAP_UNWRAP_OR),
LintId::of(&misc::USED_UNDERSCORE_BINDING),
LintId::of(&misc_early::UNSEPARATED_LITERAL_SUFFIX),
LintId::of(&mut_mut::MUT_MUT),
@ -1209,8 +1199,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&bit_mask::INEFFECTIVE_BIT_MASK),
LintId::of(&bit_mask::VERBOSE_BIT_MASK),
LintId::of(&blacklisted_name::BLACKLISTED_NAME),
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR),
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT),
LintId::of(&blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS),
LintId::of(&booleans::LOGIC_BUG),
LintId::of(&booleans::NONMINIMAL_BOOL),
LintId::of(&bytecount::NAIVE_BYTECOUNT),
@ -1274,8 +1263,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&loops::EMPTY_LOOP),
LintId::of(&loops::EXPLICIT_COUNTER_LOOP),
LintId::of(&loops::FOR_KV_MAP),
LintId::of(&loops::FOR_LOOP_OVER_OPTION),
LintId::of(&loops::FOR_LOOP_OVER_RESULT),
LintId::of(&loops::FOR_LOOPS_OVER_FALLIBLES),
LintId::of(&loops::ITER_NEXT_LOOP),
LintId::of(&loops::MANUAL_MEMCPY),
LintId::of(&loops::MUT_RANGE_BOUND),
@ -1455,8 +1443,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&attrs::UNKNOWN_CLIPPY_LINTS),
LintId::of(&bit_mask::VERBOSE_BIT_MASK),
LintId::of(&blacklisted_name::BLACKLISTED_NAME),
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR),
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT),
LintId::of(&blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS),
LintId::of(&collapsible_if::COLLAPSIBLE_IF),
LintId::of(&comparison_chain::COMPARISON_CHAIN),
LintId::of(&doc::MISSING_SAFETY_DOC),
@ -1650,8 +1637,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&inline_fn_without_body::INLINE_FN_WITHOUT_BODY),
LintId::of(&let_underscore::LET_UNDERSCORE_LOCK),
LintId::of(&literal_representation::MISTYPED_LITERAL_SUFFIXES),
LintId::of(&loops::FOR_LOOP_OVER_OPTION),
LintId::of(&loops::FOR_LOOP_OVER_RESULT),
LintId::of(&loops::FOR_LOOPS_OVER_FALLIBLES),
LintId::of(&loops::ITER_NEXT_LOOP),
LintId::of(&loops::NEVER_LOOP),
LintId::of(&loops::WHILE_IMMUTABLE_CONDITION),
@ -1798,6 +1784,17 @@ pub fn register_renamed(ls: &mut rustc_lint::LintStore) {
ls.register_renamed("clippy::new_without_default_derive", "clippy::new_without_default");
ls.register_renamed("clippy::cyclomatic_complexity", "clippy::cognitive_complexity");
ls.register_renamed("clippy::const_static_lifetime", "clippy::redundant_static_lifetimes");
ls.register_renamed("clippy::block_in_if_condition_expr", "clippy::blocks_in_if_conditions");
ls.register_renamed("clippy::block_in_if_condition_stmt", "clippy::blocks_in_if_conditions");
ls.register_renamed("clippy::option_map_unwrap_or", "clippy::map_unwrap_or");
ls.register_renamed("clippy::option_map_unwrap_or_else", "clippy::map_unwrap_or");
ls.register_renamed("clippy::result_map_unwrap_or_else", "clippy::map_unwrap_or");
ls.register_renamed("clippy::option_unwrap_used", "clippy::unwrap_used");
ls.register_renamed("clippy::result_unwrap_used", "clippy::unwrap_used");
ls.register_renamed("clippy::option_expect_used", "clippy::expect_used");
ls.register_renamed("clippy::result_expect_used", "clippy::expect_used");
ls.register_renamed("clippy::for_loop_over_option", "clippy::for_loops_over_fallibles");
ls.register_renamed("clippy::for_loop_over_result", "clippy::for_loops_over_fallibles");
}
// only exists to let the dogfood integration test works.

View File

@ -168,7 +168,7 @@ declare_clippy_lint! {
}
declare_clippy_lint! {
/// **What it does:** Checks for `for` loops over `Option` values.
/// **What it does:** Checks for `for` loops over `Option` or `Result` values.
///
/// **Why is this bad?** Readability. This is more clearly expressed as an `if
/// let`.
@ -176,47 +176,38 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```ignore
/// for x in option {
/// ..
/// ```rust
/// # let opt = Some(1);
///
/// // Bad
/// for x in opt {
/// // ..
/// }
///
/// // Good
/// if let Some(x) = opt {
/// // ..
/// }
/// ```
///
/// This should be
/// ```ignore
/// if let Some(x) = option {
/// ..
/// // or
///
/// ```rust
/// # let res: Result<i32, std::io::Error> = Ok(1);
///
/// // Bad
/// for x in &res {
/// // ..
/// }
///
/// // Good
/// if let Ok(x) = res {
/// // ..
/// }
/// ```
pub FOR_LOOP_OVER_OPTION,
pub FOR_LOOPS_OVER_FALLIBLES,
correctness,
"for-looping over an `Option`, which is more clearly expressed as an `if let`"
}
declare_clippy_lint! {
/// **What it does:** Checks for `for` loops over `Result` values.
///
/// **Why is this bad?** Readability. This is more clearly expressed as an `if
/// let`.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```ignore
/// for x in result {
/// ..
/// }
/// ```
///
/// This should be
/// ```ignore
/// if let Ok(x) = result {
/// ..
/// }
/// ```
pub FOR_LOOP_OVER_RESULT,
correctness,
"for-looping over a `Result`, which is more clearly expressed as an `if let`"
"for-looping over an `Option` or a `Result`, which is more clearly expressed as an `if let`"
}
declare_clippy_lint! {
@ -435,8 +426,7 @@ declare_lint_pass!(Loops => [
EXPLICIT_ITER_LOOP,
EXPLICIT_INTO_ITER_LOOP,
ITER_NEXT_LOOP,
FOR_LOOP_OVER_RESULT,
FOR_LOOP_OVER_OPTION,
FOR_LOOPS_OVER_FALLIBLES,
WHILE_LET_LOOP,
NEEDLESS_COLLECT,
EXPLICIT_COUNTER_LOOP,
@ -1283,7 +1273,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>, e
ITER_NEXT_LOOP,
expr.span,
"you are iterating over `Iterator::next()` which is an Option; this will compile but is \
probably not what you want",
probably not what you want",
);
next_loop_linted = true;
}
@ -1300,11 +1290,11 @@ fn check_arg_type(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>) {
if is_type_diagnostic_item(cx, ty, sym!(option_type)) {
span_lint_and_help(
cx,
FOR_LOOP_OVER_OPTION,
FOR_LOOPS_OVER_FALLIBLES,
arg.span,
&format!(
"for loop over `{0}`, which is an `Option`. This is more readably written as an \
`if let` statement.",
`if let` statement.",
snippet(cx, arg.span, "_")
),
None,
@ -1317,11 +1307,11 @@ fn check_arg_type(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>) {
} else if is_type_diagnostic_item(cx, ty, sym!(result_type)) {
span_lint_and_help(
cx,
FOR_LOOP_OVER_RESULT,
FOR_LOOPS_OVER_FALLIBLES,
arg.span,
&format!(
"for loop over `{0}`, which is a `Result`. This is more readably written as an \
`if let` statement.",
`if let` statement.",
snippet(cx, arg.span, "_")
),
None,

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,81 +50,73 @@ 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! {
/// **What it does:** Checks for `.expect()` calls on `Option`s.
/// **What it does:** Checks for `.expect()` calls on `Option`s and `Result`s.
///
/// **Why is this bad?** Usually it is better to handle the `None` case. Still,
/// for a lot of quick-and-dirty code, `expect` is a good choice, which is why
/// this lint is `Allow` by default.
/// **Why is this bad?** Usually it is better to handle the `None` or `Err` case.
/// Still, for a lot of quick-and-dirty code, `expect` is a good choice, which is why
/// this lint is `Allow` by default.
///
/// **Known problems:** None.
///
/// **Example:**
///
/// Using expect on an `Option`:
///
/// ```rust
/// let opt = Some(1);
/// opt.expect("one");
/// ```
///
/// Better:
///
/// ```rust,ignore
/// let opt = Some(1);
/// opt?;
/// ```
pub OPTION_EXPECT_USED,
restriction,
"using `Option.expect()`, which might be better handled"
}
declare_clippy_lint! {
/// **What it does:** Checks for `.expect()` calls on `Result`s.
///
/// **Why is this bad?** `result.expect()` will let the thread panic on `Err`
/// `result.expect()` will let the thread panic on `Err`
/// values. Normally, you want to implement more sophisticated error handling,
/// and propagate errors upwards with `?` operator.
///
/// **Known problems:** None.
///
/// **Example:**
/// Using expect on an `Result`:
/// **Examples:**
/// ```rust,ignore
/// # let opt = Some(1);
///
/// ```rust
/// let res: Result<usize, ()> = Ok(1);
/// res.expect("one");
/// // Bad
/// opt.expect("one");
///
/// // Good
/// let opt = Some(1);
/// opt?;
/// ```
///
/// Better:
/// // or
///
/// ```rust
/// let res: Result<usize, ()> = Ok(1);
/// # let res: Result<usize, ()> = Ok(1);
///
/// // Bad
/// res.expect("one");
///
/// // Good
/// res?;
/// # Ok::<(), ()>(())
/// ```
pub RESULT_EXPECT_USED,
pub EXPECT_USED,
restriction,
"using `Result.expect()`, which might be better handled"
"using `.expect()` on `Result` or `Option`, which might be better handled"
}
declare_clippy_lint! {
@ -257,59 +224,40 @@ declare_clippy_lint! {
}
declare_clippy_lint! {
/// **What it does:** Checks for usage of `_.map(_).unwrap_or(_)`.
/// **What it does:** Checks for usage of `option.map(_).unwrap_or(_)` or `option.map(_).unwrap_or_else(_)` or
/// `result.map(_).unwrap_or_else(_)`.
///
/// **Why is this bad?** Readability, this can be written more concisely as
/// `_.map_or(_, _)`.
/// **Why is this bad?** Readability, these can be written more concisely (resp.) as
/// `option.map_or(_, _)`, `option.map_or_else(_, _)` and `result.map_or_else(_, _)`.
///
/// **Known problems:** The order of the arguments is not in execution order
///
/// **Example:**
/// **Examples:**
/// ```rust
/// # let x = Some(1);
///
/// // Bad
/// x.map(|a| a + 1).unwrap_or(0);
///
/// // Good
/// x.map_or(0, |a| a + 1);
/// ```
pub OPTION_MAP_UNWRAP_OR,
pedantic,
"using `Option.map(f).unwrap_or(a)`, which is more succinctly expressed as `map_or(a, f)`"
}
declare_clippy_lint! {
/// **What it does:** Checks for usage of `_.map(_).unwrap_or_else(_)`.
///
/// **Why is this bad?** Readability, this can be written more concisely as
/// `_.map_or_else(_, _)`.
/// // or
///
/// **Known problems:** The order of the arguments is not in execution order.
///
/// **Example:**
/// ```rust
/// # let x = Some(1);
/// # fn some_function() -> usize { 1 }
/// x.map(|a| a + 1).unwrap_or_else(some_function);
/// ```
pub OPTION_MAP_UNWRAP_OR_ELSE,
pedantic,
"using `Option.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `map_or_else(g, f)`"
}
declare_clippy_lint! {
/// **What it does:** Checks for usage of `result.map(_).unwrap_or_else(_)`.
///
/// **Why is this bad?** Readability, this can be written more concisely as
/// `result.map_or_else(_, _)`.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// # let x: Result<usize, ()> = Ok(1);
/// # fn some_function(foo: ()) -> usize { 1 }
///
/// // Bad
/// x.map(|a| a + 1).unwrap_or_else(some_function);
///
/// // Good
/// x.map_or_else(some_function, |a| a + 1);
/// ```
pub RESULT_MAP_UNWRAP_OR_ELSE,
pub MAP_UNWRAP_OR,
pedantic,
"using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `.map_or_else(g, f)`"
"using `.map(f).unwrap_or(a)` or `.map(f).unwrap_or_else(func)`, which are more succinctly expressed as `map_or(a, f)` or `map_or_else(a, f)`"
}
declare_clippy_lint! {
@ -1286,17 +1234,13 @@ declare_clippy_lint! {
}
declare_lint_pass!(Methods => [
OPTION_UNWRAP_USED,
RESULT_UNWRAP_USED,
OPTION_EXPECT_USED,
RESULT_EXPECT_USED,
UNWRAP_USED,
EXPECT_USED,
SHOULD_IMPLEMENT_TRAIT,
WRONG_SELF_CONVENTION,
WRONG_PUB_SELF_CONVENTION,
OK_EXPECT,
OPTION_MAP_UNWRAP_OR,
OPTION_MAP_UNWRAP_OR_ELSE,
RESULT_MAP_UNWRAP_OR_ELSE,
MAP_UNWRAP_OR,
RESULT_MAP_OR_INTO_OPTION,
OPTION_MAP_OR_NONE,
OPTION_AND_THEN_SOME,
@ -1503,9 +1447,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
cx,
lint,
first_arg.pat.span,
&format!(
"methods called `{}` usually take {}; consider choosing a less \
ambiguous name",
&format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
conv,
&self_kinds
.iter()
@ -1678,7 +1620,7 @@ fn lint_or_fun_call<'a, 'tcx>(
let self_ty = cx.tables.expr_ty(self_expr);
if let Some(&(_, fn_has_arguments, poss, suffix)) =
know_types.iter().find(|&&i| match_type(cx, self_ty, i.0));
know_types.iter().find(|&&i| match_type(cx, self_ty, i.0));
if poss.contains(&name);
@ -1931,7 +1873,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
CLONE_DOUBLE_REF,
expr.span,
"using `clone` on a double-reference; \
this will copy the reference instead of cloning the inner type",
this will copy the reference instead of cloning the inner type",
|diag| {
if let Some(snip) = sugg::Sugg::hir_opt(cx, arg) {
let mut ty = innermost;
@ -2121,7 +2063,7 @@ fn lint_iter_cloned_collect<'a, 'tcx>(
ITER_CLONED_COLLECT,
to_replace,
"called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
more readable",
more readable",
"try",
".to_vec()".to_string(),
Applicability::MachineApplicable,
@ -2420,9 +2362,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
};
@ -2436,7 +2378,7 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
None,
&format!(
"if you don't want to handle the `{}` case gracefully, consider \
using `expect()` to provide a better panic message",
using `expect()` to provide a better panic message",
none_value,
),
);
@ -2448,9 +2390,9 @@ fn lint_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, expect_args: &[hi
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&expect_args[0]));
let mess = if is_type_diagnostic_item(cx, obj_ty, sym!(option_type)) {
Some((OPTION_EXPECT_USED, "an Option", "None"))
Some((EXPECT_USED, "an Option", "None"))
} else if is_type_diagnostic_item(cx, obj_ty, sym!(result_type)) {
Some((RESULT_EXPECT_USED, "a Result", "Err"))
Some((EXPECT_USED, "a Result", "Err"))
} else {
None
};
@ -2494,7 +2436,7 @@ fn lint_map_flatten<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<
// lint if caller of `.map().flatten()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `map(..).flatten()` on an `Iterator`. \
This is more succinctly expressed by calling `.flat_map(..)`";
This is more succinctly expressed by calling `.flat_map(..)`";
let self_snippet = snippet(cx, map_args[0].span, "..");
let func_snippet = snippet(cx, map_args[1].span, "..");
let hint = format!("{0}.flat_map({1})", self_snippet, func_snippet);
@ -2555,10 +2497,10 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
// lint message
let msg = if is_option {
"called `map(f).unwrap_or_else(g)` on an `Option` value. This can be done more directly by calling \
`map_or_else(g, f)` instead"
`map_or_else(g, f)` instead"
} else {
"called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling \
`.map_or_else(g, f)` instead"
`.map_or_else(g, f)` instead"
};
// get snippets for args to map() and unwrap_or_else()
let map_snippet = snippet(cx, map_args[1].span, "..");
@ -2570,11 +2512,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
if same_span && !multiline {
span_lint_and_note(
cx,
if is_option {
OPTION_MAP_UNWRAP_OR_ELSE
} else {
RESULT_MAP_UNWRAP_OR_ELSE
},
MAP_UNWRAP_OR,
expr.span,
msg,
None,
@ -2584,16 +2522,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
),
);
} else if same_span && multiline {
span_lint(
cx,
if is_option {
OPTION_MAP_UNWRAP_OR_ELSE
} else {
RESULT_MAP_UNWRAP_OR_ELSE
},
expr.span,
msg,
);
span_lint(cx, MAP_UNWRAP_OR, expr.span, msg);
};
}
}

View File

@ -9,7 +9,7 @@ use rustc_middle::hir::map::Map;
use rustc_span::source_map::Span;
use rustc_span::symbol::Symbol;
use super::OPTION_MAP_UNWRAP_OR;
use super::MAP_UNWRAP_OR;
/// lint use of `map().unwrap_or()` for `Option`s
pub(super) fn lint<'a, 'tcx>(
@ -62,11 +62,11 @@ pub(super) fn lint<'a, 'tcx>(
};
let msg = &format!(
"called `map(f).unwrap_or({})` on an `Option` value. \
This can be done more directly by calling `{}` instead",
This can be done more directly by calling `{}` instead",
arg, suggest
);
span_lint_and_then(cx, OPTION_MAP_UNWRAP_OR, expr.span, msg, |diag| {
span_lint_and_then(cx, MAP_UNWRAP_OR, expr.span, msg, |diag| {
let map_arg_span = map_args[1].span;
let mut suggestion = vec![

View File

@ -74,18 +74,11 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
module: "blacklisted_name",
},
Lint {
name: "block_in_if_condition_expr",
name: "blocks_in_if_conditions",
group: "style",
desc: "braces that can be eliminated in conditions, e.g., `if { true } ...`",
desc: "useless or complex blocks that can be eliminated in conditions",
deprecation: None,
module: "block_in_if_condition",
},
Lint {
name: "block_in_if_condition_stmt",
group: "style",
desc: "complex blocks in conditions, e.g., `if { let x = true; x } ...`",
deprecation: None,
module: "block_in_if_condition",
module: "blocks_in_if_conditions",
},
Lint {
name: "bool_comparison",
@ -521,6 +514,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
deprecation: None,
module: "methods",
},
Lint {
name: "expect_used",
group: "restriction",
desc: "using `.expect()` on `Result` or `Option`, which might be better handled",
deprecation: None,
module: "methods",
},
Lint {
name: "expl_impl_clone_on_copy",
group: "pedantic",
@ -676,16 +676,9 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
module: "loops",
},
Lint {
name: "for_loop_over_option",
name: "for_loops_over_fallibles",
group: "correctness",
desc: "for-looping over an `Option`, which is more clearly expressed as an `if let`",
deprecation: None,
module: "loops",
},
Lint {
name: "for_loop_over_result",
group: "correctness",
desc: "for-looping over a `Result`, which is more clearly expressed as an `if let`",
desc: "for-looping over an `Option` or a `Result`, which is more clearly expressed as an `if let`",
deprecation: None,
module: "loops",
},
@ -1144,6 +1137,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
deprecation: None,
module: "methods",
},
Lint {
name: "map_unwrap_or",
group: "pedantic",
desc: "using `.map(f).unwrap_or(a)` or `.map(f).unwrap_or_else(func)`, which are more succinctly expressed as `map_or(a, f)` or `map_or_else(a, f)`",
deprecation: None,
module: "methods",
},
Lint {
name: "match_as_ref",
group: "complexity",
@ -1599,13 +1599,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
deprecation: None,
module: "option_env_unwrap",
},
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",
@ -1620,20 +1613,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
deprecation: None,
module: "map_unit_fn",
},
Lint {
name: "option_map_unwrap_or",
group: "pedantic",
desc: "using `Option.map(f).unwrap_or(a)`, which is more succinctly expressed as `map_or(a, f)`",
deprecation: None,
module: "methods",
},
Lint {
name: "option_map_unwrap_or_else",
group: "pedantic",
desc: "using `Option.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `map_or_else(g, f)`",
deprecation: None,
module: "methods",
},
Lint {
name: "option_option",
group: "pedantic",
@ -1641,13 +1620,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",
@ -1886,13 +1858,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
deprecation: None,
module: "matches",
},
Lint {
name: "result_expect_used",
group: "restriction",
desc: "using `Result.expect()`, which might be better handled",
deprecation: None,
module: "methods",
},
Lint {
name: "result_map_or_into_option",
group: "style",
@ -1907,20 +1872,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
deprecation: None,
module: "map_unit_fn",
},
Lint {
name: "result_map_unwrap_or_else",
group: "pedantic",
desc: "using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `.map_or_else(g, f)`",
deprecation: None,
module: "methods",
},
Lint {
name: "result_unwrap_used",
group: "restriction",
desc: "using `Result.unwrap()`, which might be better handled",
deprecation: None,
module: "methods",
},
Lint {
name: "reversed_empty_ranges",
group: "correctness",
@ -2425,6 +2376,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,6 +1,5 @@
// run-rustfix
#![warn(clippy::block_in_if_condition_expr)]
#![warn(clippy::block_in_if_condition_stmt)]
#![warn(clippy::blocks_in_if_conditions)]
#![allow(unused, clippy::let_and_return)]
#![warn(clippy::nonminimal_bool)]

View File

@ -1,6 +1,5 @@
// run-rustfix
#![warn(clippy::block_in_if_condition_expr)]
#![warn(clippy::block_in_if_condition_stmt)]
#![warn(clippy::blocks_in_if_conditions)]
#![allow(unused, clippy::let_and_return)]
#![warn(clippy::nonminimal_bool)]

View File

@ -1,5 +1,5 @@
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
--> $DIR/block_in_if_condition.rs:27:5
--> $DIR/blocks_in_if_conditions.rs:26:5
|
LL | / if {
LL | | let x = 3;
@ -7,7 +7,7 @@ LL | | x == 3
LL | | } {
| |_____^
|
= note: `-D clippy::block-in-if-condition-stmt` implied by `-D warnings`
= note: `-D clippy::blocks-in-if-conditions` implied by `-D warnings`
help: try
|
LL | let res = {
@ -17,15 +17,13 @@ LL | }; if res {
|
error: omit braces around single expression condition
--> $DIR/block_in_if_condition.rs:38:8
--> $DIR/blocks_in_if_conditions.rs:37:8
|
LL | if { true } {
| ^^^^^^^^ help: try: `true`
|
= note: `-D clippy::block-in-if-condition-expr` implied by `-D warnings`
error: this boolean expression can be simplified
--> $DIR/block_in_if_condition.rs:47:8
--> $DIR/blocks_in_if_conditions.rs:46:8
|
LL | if true && x == 3 {
| ^^^^^^^^^^^^^^ help: try: `x == 3`

View File

@ -1,5 +1,4 @@
#![warn(clippy::block_in_if_condition_expr)]
#![warn(clippy::block_in_if_condition_stmt)]
#![warn(clippy::blocks_in_if_conditions)]
#![allow(unused, clippy::let_and_return)]
fn predicate<F: FnOnce(T) -> bool, T>(pfn: F, val: T) -> bool {
@ -10,7 +9,7 @@ fn pred_test() {
let v = 3;
let sky = "blue";
// This is a sneaky case, where the block isn't directly in the condition,
// but is actually nside a closure that the condition is using.
// but is actually inside a closure that the condition is using.
// The same principle applies -- add some extra expressions to make sure
// linter isn't confused by them.
if v == 3

View File

@ -1,5 +1,5 @@
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
--> $DIR/block_in_if_condition_closure.rs:19:17
--> $DIR/blocks_in_if_conditions_closure.rs:18:17
|
LL | |x| {
| _________________^
@ -8,10 +8,10 @@ LL | | x == target
LL | | },
| |_____________^
|
= note: `-D clippy::block-in-if-condition-stmt` implied by `-D warnings`
= note: `-D clippy::blocks-in-if-conditions` implied by `-D warnings`
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
--> $DIR/block_in_if_condition_closure.rs:28:13
--> $DIR/blocks_in_if_conditions_closure.rs:27:13
|
LL | |x| {
| _____________^

View File

@ -1,4 +1,4 @@
#![warn(clippy::option_expect_used, clippy::result_expect_used)]
#![warn(clippy::expect_used)]
fn expect_option() {
let opt = Some(0);

View File

@ -4,7 +4,7 @@ error: used `expect()` on `an Option` value
LL | let _ = opt.expect("");
| ^^^^^^^^^^^^^^
|
= note: `-D clippy::option-expect-used` implied by `-D warnings`
= note: `-D clippy::expect-used` implied by `-D warnings`
= help: if this value is an `None`, it will panic
error: used `expect()` on `a Result` value
@ -13,7 +13,6 @@ error: used `expect()` on `a Result` value
LL | let _ = res.expect("");
| ^^^^^^^^^^^^^^
|
= note: `-D clippy::result-expect-used` implied by `-D warnings`
= help: if this value is an `Err`, it will panic
error: aborting due to 2 previous errors

View File

@ -1,18 +1,16 @@
#![warn(clippy::for_loop_over_option, clippy::for_loop_over_result)]
#![warn(clippy::for_loops_over_fallibles)]
/// Tests for_loop_over_result and for_loop_over_option
fn for_loop_over_option_and_result() {
fn for_loops_over_fallibles() {
let option = Some(1);
let result = option.ok_or("x not found");
let v = vec![0, 1, 2];
// check FOR_LOOP_OVER_OPTION lint
// check over an `Option`
for x in option {
println!("{}", x);
}
// check FOR_LOOP_OVER_RESULT lint
// check over a `Result`
for x in result {
println!("{}", x);
}

View File

@ -1,23 +1,22 @@
error: for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement.
--> $DIR/for_loop_over_option_result.rs:11:14
--> $DIR/for_loops_over_fallibles.rs:9:14
|
LL | for x in option {
| ^^^^^^
|
= note: `-D clippy::for-loop-over-option` implied by `-D warnings`
= note: `-D clippy::for-loops-over-fallibles` implied by `-D warnings`
= help: consider replacing `for x in option` with `if let Some(x) = option`
error: for loop over `result`, which is a `Result`. This is more readably written as an `if let` statement.
--> $DIR/for_loop_over_option_result.rs:16:14
--> $DIR/for_loops_over_fallibles.rs:14:14
|
LL | for x in result {
| ^^^^^^
|
= note: `-D clippy::for-loop-over-result` implied by `-D warnings`
= help: consider replacing `for x in result` with `if let Ok(x) = result`
error: for loop over `option.ok_or("x not found")`, which is a `Result`. This is more readably written as an `if let` statement.
--> $DIR/for_loop_over_option_result.rs:20:14
--> $DIR/for_loops_over_fallibles.rs:18:14
|
LL | for x in option.ok_or("x not found") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -25,7 +24,7 @@ LL | for x in option.ok_or("x not found") {
= help: consider replacing `for x in option.ok_or("x not found")` with `if let Ok(x) = option.ok_or("x not found")`
error: you are iterating over `Iterator::next()` which is an Option; this will compile but is probably not what you want
--> $DIR/for_loop_over_option_result.rs:26:14
--> $DIR/for_loops_over_fallibles.rs:24:14
|
LL | for x in v.iter().next() {
| ^^^^^^^^^^^^^^^
@ -33,7 +32,7 @@ LL | for x in v.iter().next() {
= note: `#[deny(clippy::iter_next_loop)]` on by default
error: for loop over `v.iter().next().and(Some(0))`, which is an `Option`. This is more readably written as an `if let` statement.
--> $DIR/for_loop_over_option_result.rs:31:14
--> $DIR/for_loops_over_fallibles.rs:29:14
|
LL | for x in v.iter().next().and(Some(0)) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -41,7 +40,7 @@ LL | for x in v.iter().next().and(Some(0)) {
= help: consider replacing `for x in v.iter().next().and(Some(0))` with `if let Some(x) = v.iter().next().and(Some(0))`
error: for loop over `v.iter().next().ok_or("x not found")`, which is a `Result`. This is more readably written as an `if let` statement.
--> $DIR/for_loop_over_option_result.rs:35:14
--> $DIR/for_loops_over_fallibles.rs:33:14
|
LL | for x in v.iter().next().ok_or("x not found") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -49,7 +48,7 @@ LL | for x in v.iter().next().ok_or("x not found") {
= help: consider replacing `for x in v.iter().next().ok_or("x not found")` with `if let Ok(x) = v.iter().next().ok_or("x not found")`
error: this loop never actually loops
--> $DIR/for_loop_over_option_result.rs:47:5
--> $DIR/for_loops_over_fallibles.rs:45:5
|
LL | / while let Some(x) = option {
LL | | println!("{}", x);
@ -60,7 +59,7 @@ LL | | }
= note: `#[deny(clippy::never_loop)]` on by default
error: this loop never actually loops
--> $DIR/for_loop_over_option_result.rs:53:5
--> $DIR/for_loops_over_fallibles.rs:51:5
|
LL | / while let Ok(x) = result {
LL | | println!("{}", x);

View File

@ -1,21 +1,18 @@
// FIXME: Add "run-rustfix" once it's supported for multipart suggestions
// aux-build:option_helpers.rs
#![warn(clippy::option_map_unwrap_or, clippy::option_map_unwrap_or_else)]
#![warn(clippy::map_unwrap_or)]
#[macro_use]
extern crate option_helpers;
use std::collections::HashMap;
/// Checks implementation of the following lints:
/// * `OPTION_MAP_UNWRAP_OR`
/// * `OPTION_MAP_UNWRAP_OR_ELSE`
#[rustfmt::skip]
fn option_methods() {
let opt = Some(1);
// Check `OPTION_MAP_UNWRAP_OR`.
// Check for `option.map(_).unwrap_or(_)` use.
// Single line case.
let _ = opt.map(|x| x + 1)
// Should lint even though this call is on a separate line.
@ -49,7 +46,7 @@ fn option_methods() {
let id: String = "identifier".to_string();
let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
// Check OPTION_MAP_UNWRAP_OR_ELSE
// Check for `option.map(_).unwrap_or_else(_)` use.
// single line case
let _ = opt.map(|x| x + 1)
// Should lint even though this call is on a separate line.
@ -83,6 +80,20 @@ fn option_methods() {
}
}
fn result_methods() {
let res: Result<i32, ()> = Ok(1);
// Check for `result.map(_).unwrap_or_else(_)` use.
// single line case
let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
// multi line cases
let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
// macro case
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint
}
fn main() {
option_methods();
result_methods();
}

View File

@ -1,5 +1,5 @@
error: called `map(f).unwrap_or(a)` on an `Option` value. This can be done more directly by calling `map_or(a, f)` instead
--> $DIR/option_map_unwrap_or.rs:20:13
--> $DIR/map_unwrap_or.rs:17:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
@ -7,14 +7,14 @@ LL | | // Should lint even though this call is on a separate line.
LL | | .unwrap_or(0);
| |_____________________^
|
= note: `-D clippy::option-map-unwrap-or` implied by `-D warnings`
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
help: use `map_or(a, f)` instead
|
LL | let _ = opt.map_or(0, |x| x + 1);
| ^^^^^^ ^^ --
error: called `map(f).unwrap_or(a)` on an `Option` value. This can be done more directly by calling `map_or(a, f)` instead
--> $DIR/option_map_unwrap_or.rs:24:13
--> $DIR/map_unwrap_or.rs:21:13
|
LL | let _ = opt.map(|x| {
| _____________^
@ -32,7 +32,7 @@ LL | );
|
error: called `map(f).unwrap_or(a)` on an `Option` value. This can be done more directly by calling `map_or(a, f)` instead
--> $DIR/option_map_unwrap_or.rs:28:13
--> $DIR/map_unwrap_or.rs:25:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
@ -49,7 +49,7 @@ LL | }, |x| x + 1);
|
error: called `map(f).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(f)` instead
--> $DIR/option_map_unwrap_or.rs:33:13
--> $DIR/map_unwrap_or.rs:30:13
|
LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -60,7 +60,7 @@ LL | let _ = opt.and_then(|x| Some(x + 1));
| ^^^^^^^^ --
error: called `map(f).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(f)` instead
--> $DIR/option_map_unwrap_or.rs:35:13
--> $DIR/map_unwrap_or.rs:32:13
|
LL | let _ = opt.map(|x| {
| _____________^
@ -78,7 +78,7 @@ LL | );
|
error: called `map(f).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(f)` instead
--> $DIR/option_map_unwrap_or.rs:39:13
--> $DIR/map_unwrap_or.rs:36:13
|
LL | let _ = opt
| _____________^
@ -92,7 +92,7 @@ LL | .and_then(|x| Some(x + 1));
| ^^^^^^^^ --
error: called `map(f).unwrap_or(a)` on an `Option` value. This can be done more directly by calling `map_or(a, f)` instead
--> $DIR/option_map_unwrap_or.rs:50:13
--> $DIR/map_unwrap_or.rs:47:13
|
LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -103,7 +103,7 @@ LL | let _ = Some("prefix").map_or(id, |p| format!("{}.", p));
| ^^^^^^ ^^^ --
error: called `map(f).unwrap_or_else(g)` on an `Option` value. This can be done more directly by calling `map_or_else(g, f)` instead
--> $DIR/option_map_unwrap_or.rs:54:13
--> $DIR/map_unwrap_or.rs:51:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
@ -111,11 +111,10 @@ LL | | // Should lint even though this call is on a separate line.
LL | | .unwrap_or_else(|| 0);
| |_____________________________^
|
= note: `-D clippy::option-map-unwrap-or-else` implied by `-D warnings`
= note: replace `map(|x| x + 1).unwrap_or_else(|| 0)` with `map_or_else(|| 0, |x| x + 1)`
error: called `map(f).unwrap_or_else(g)` on an `Option` value. This can be done more directly by calling `map_or_else(g, f)` instead
--> $DIR/option_map_unwrap_or.rs:58:13
--> $DIR/map_unwrap_or.rs:55:13
|
LL | let _ = opt.map(|x| {
| _____________^
@ -125,7 +124,7 @@ LL | | ).unwrap_or_else(|| 0);
| |__________________________^
error: called `map(f).unwrap_or_else(g)` on an `Option` value. This can be done more directly by calling `map_or_else(g, f)` instead
--> $DIR/option_map_unwrap_or.rs:62:13
--> $DIR/map_unwrap_or.rs:59:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
@ -134,5 +133,29 @@ LL | | 0
LL | | );
| |_________^
error: aborting due to 10 previous errors
error: called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling `.map_or_else(g, f)` instead
--> $DIR/map_unwrap_or.rs:88:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`
error: called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling `.map_or_else(g, f)` instead
--> $DIR/map_unwrap_or.rs:90:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`
error: called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling `.map_or_else(g, f)` instead
--> $DIR/map_unwrap_or.rs:91:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`
error: aborting due to 13 previous errors

View File

@ -1,23 +0,0 @@
// aux-build:option_helpers.rs
//! Checks implementation of `RESULT_MAP_UNWRAP_OR_ELSE`
#![warn(clippy::result_map_unwrap_or_else)]
#[macro_use]
extern crate option_helpers;
fn result_methods() {
let res: Result<i32, ()> = Ok(1);
// Check RESULT_MAP_UNWRAP_OR_ELSE
// single line case
let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
// multi line cases
let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
// macro case
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint
}
fn main() {}

View File

@ -1,27 +0,0 @@
error: called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling `.map_or_else(g, f)` instead
--> $DIR/result_map_unwrap_or_else.rs:15:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::result-map-unwrap-or-else` implied by `-D warnings`
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`
error: called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling `.map_or_else(g, f)` instead
--> $DIR/result_map_unwrap_or_else.rs:17:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`
error: called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling `.map_or_else(g, f)` instead
--> $DIR/result_map_unwrap_or_else.rs:18:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`
error: aborting due to 3 previous errors

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