Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will catch `unwrap_or_else(Default::default)` on Result and Option and suggest `unwrap_or_default()` instead.
- Loading branch information
Showing
13 changed files
with
356 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//! Lint for `some_result_or_option.unwrap_or_else(Default::default)` | ||
|
||
use super::UNWRAP_OR_ELSE_DEFAULT; | ||
use clippy_utils::{ | ||
diagnostics::span_lint_and_sugg, | ||
source::snippet_with_applicability, | ||
ty::{is_type_diagnostic_item, qpath_target_trait}, | ||
}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'_>, | ||
recv: &'tcx hir::Expr<'_>, | ||
u_arg: &'tcx hir::Expr<'_>, | ||
) { | ||
// something.unwrap_or_else(Default::default) | ||
// ^^^^^^^^^- recv ^^^^^^^^^^^^^^^^- u_arg | ||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- expr | ||
let recv_ty = cx.typeck_results().expr_ty(recv); | ||
let is_option = is_type_diagnostic_item(cx, recv_ty, sym::option_type); | ||
let is_result = is_type_diagnostic_item(cx, recv_ty, sym::result_type); | ||
|
||
if_chain! { | ||
if is_option || is_result; | ||
if let hir::ExprKind::Path(ref qpath) = u_arg.kind; | ||
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default); | ||
if let Some(target_trait) = qpath_target_trait(cx, qpath, u_arg.hir_id); | ||
if target_trait == default_trait_id; | ||
then { | ||
let mut applicability = Applicability::MachineApplicable; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
UNWRAP_OR_ELSE_DEFAULT, | ||
expr.span, | ||
"use of `.unwrap_or_else(..)` to construct default value", | ||
"try", | ||
format!( | ||
"{}.unwrap_or_default()", | ||
snippet_with_applicability(cx, recv.span, "..", &mut applicability) | ||
), | ||
applicability, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.