Skip to content

Commit

Permalink
Rename into manual_unwrap_or_default
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Mar 10, 2024
1 parent b0f358f commit 98ac5f1
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -5377,6 +5377,7 @@ Released 2018-09-13
[`manual_swap`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_swap
[`manual_try_fold`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_try_fold
[`manual_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or
[`manual_unwrap_or_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or_default
[`manual_while_let_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_while_let_some
[`many_single_char_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#many_single_char_names
[`map_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
Expand All @@ -5390,7 +5391,6 @@ Released 2018-09-13
[`match_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_bool
[`match_like_matches_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro
[`match_on_vec_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_on_vec_items
[`match_option_and_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_option_and_default
[`match_overlapping_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_overlapping_arm
[`match_ref_pats`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_ref_pats
[`match_result_ok`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_result_ok
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/declared_lints.rs
Expand Up @@ -310,9 +310,9 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::manual_slice_size_calculation::MANUAL_SLICE_SIZE_CALCULATION_INFO,
crate::manual_string_new::MANUAL_STRING_NEW_INFO,
crate::manual_strip::MANUAL_STRIP_INFO,
crate::manual_unwrap_or_default::MANUAL_UNWRAP_OR_DEFAULT_INFO,
crate::map_unit_fn::OPTION_MAP_UNIT_FN_INFO,
crate::map_unit_fn::RESULT_MAP_UNIT_FN_INFO,
crate::match_option_and_default::MATCH_OPTION_AND_DEFAULT_INFO,
crate::match_result_ok::MATCH_RESULT_OK_INFO,
crate::matches::COLLAPSIBLE_MATCH_INFO,
crate::matches::INFALLIBLE_DESTRUCTURING_MATCH_INFO,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/lib.rs
Expand Up @@ -211,8 +211,8 @@ mod manual_retain;
mod manual_slice_size_calculation;
mod manual_string_new;
mod manual_strip;
mod manual_unwrap_or_default;
mod map_unit_fn;
mod match_option_and_default;
mod match_result_ok;
mod matches;
mod mem_replace;
Expand Down Expand Up @@ -1123,7 +1123,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_early_pass(|| Box::new(multiple_bound_locations::MultipleBoundLocations));
store.register_late_pass(|_| Box::new(assigning_clones::AssigningClones));
store.register_late_pass(|_| Box::new(zero_repeat_side_effects::ZeroRepeatSideEffects));
store.register_late_pass(|_| Box::new(match_option_and_default::MatchOptionAndDefault));
store.register_late_pass(|_| Box::new(manual_unwrap_or_default::ManualUnwrapOrDefault));
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
Expand Up @@ -42,12 +42,12 @@ declare_clippy_lint! {
/// let y: Vec<String> = x.unwrap_or_default();
/// ```
#[clippy::version = "1.78.0"]
pub MATCH_OPTION_AND_DEFAULT,
pub MANUAL_UNWRAP_OR_DEFAULT,
suspicious,
"check if a `match` or `if let` can be simplified with `unwrap_or_default`"
}

declare_lint_pass!(MatchOptionAndDefault => [MATCH_OPTION_AND_DEFAULT]);
declare_lint_pass!(ManualUnwrapOrDefault => [MANUAL_UNWRAP_OR_DEFAULT]);

fn get_some<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>) -> Option<HirId> {
if let PatKind::TupleStruct(QPath::Resolved(_, path), &[pat], _) = pat.kind
Expand Down Expand Up @@ -123,13 +123,12 @@ fn handle_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
&& local_id == binding_id
// We now check the `None` arm is calling a method equivalent to `Default::default`.
&& let body_none = body_none.peel_blocks()
&& let ExprKind::Call(_, &[]) = body_none.kind
&& is_default_equivalent(cx, body_none)
&& let Some(match_expr_snippet) = snippet_opt(cx, match_expr.span)
{
span_lint_and_sugg(
cx,
MATCH_OPTION_AND_DEFAULT,
MANUAL_UNWRAP_OR_DEFAULT,
expr.span,
"match can be simplified with `.unwrap_or_default()`",
"replace it with",
Expand All @@ -155,13 +154,12 @@ fn handle_if_let<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
&& local_id == binding_id
// We now check the `None` arm is calling a method equivalent to `Default::default`.
&& let body_else = else_expr.peel_blocks()
&& let ExprKind::Call(_, &[]) = body_else.kind
&& is_default_equivalent(cx, body_else)
&& let Some(if_let_expr_snippet) = snippet_opt(cx, let_.init.span)
{
span_lint_and_sugg(
cx,
MATCH_OPTION_AND_DEFAULT,
MANUAL_UNWRAP_OR_DEFAULT,
expr.span,
"if let can be simplified with `.unwrap_or_default()`",
"replace it with",
Expand All @@ -171,7 +169,7 @@ fn handle_if_let<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
}
}

impl<'tcx> LateLintPass<'tcx> for MatchOptionAndDefault {
impl<'tcx> LateLintPass<'tcx> for ManualUnwrapOrDefault {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if expr.span.from_expansion() {
return;
Expand Down
@@ -1,4 +1,4 @@
#![warn(clippy::match_option_and_default)]
#![warn(clippy::manual_unwrap_or_default)]
#![allow(clippy::unnecessary_literal_unwrap)]

fn main() {
Expand Down
@@ -1,4 +1,4 @@
#![warn(clippy::match_option_and_default)]
#![warn(clippy::manual_unwrap_or_default)]
#![allow(clippy::unnecessary_literal_unwrap)]

fn main() {
Expand Down
@@ -1,5 +1,5 @@
error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:6:5
--> tests/ui/manual_unwrap_or_default.rs:6:5
|
LL | / match x {
LL | |
Expand All @@ -8,11 +8,11 @@ LL | | None => Vec::default(),
LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`
|
= note: `-D clippy::match-option-and-default` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::match_option_and_default)]`
= note: `-D clippy::manual-unwrap-or-default` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::manual_unwrap_or_default)]`

error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:13:5
--> tests/ui/manual_unwrap_or_default.rs:13:5
|
LL | / match x {
LL | |
Expand All @@ -22,7 +22,7 @@ LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:20:5
--> tests/ui/manual_unwrap_or_default.rs:20:5
|
LL | / match x {
LL | |
Expand All @@ -32,7 +32,7 @@ LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:27:5
--> tests/ui/manual_unwrap_or_default.rs:27:5
|
LL | / match x {
LL | |
Expand All @@ -42,7 +42,7 @@ LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: if let can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:34:5
--> tests/ui/manual_unwrap_or_default.rs:34:5
|
LL | / if let Some(v) = x {
LL | |
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/option_option.rs
@@ -1,7 +1,7 @@
//@compile-flags: -Zdeduplicate-diagnostics=yes

#![deny(clippy::option_option)]
#![allow(clippy::unnecessary_wraps, clippy::match_option_and_default)]
#![allow(clippy::unnecessary_wraps, clippy::manual_unwrap_or_default)]

const C: Option<Option<i32>> = None;
//~^ ERROR: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if
Expand Down

0 comments on commit 98ac5f1

Please sign in to comment.