-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
GetFirst
into Methods
lint pass
- Loading branch information
Showing
7 changed files
with
76 additions
and
75 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_slice_of_primitives; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use if_chain::if_chain; | ||
use rustc_ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::source_map::Spanned; | ||
|
||
use super::GET_FIRST; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'_>, | ||
recv: &'tcx hir::Expr<'_>, | ||
arg: &'tcx hir::Expr<'_>, | ||
) { | ||
if_chain! { | ||
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); | ||
if let Some(impl_id) = cx.tcx.impl_of_method(method_id); | ||
if cx.tcx.type_of(impl_id).is_slice(); | ||
if let Some(_) = is_slice_of_primitives(cx, recv); | ||
if let hir::ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = arg.kind; | ||
then { | ||
let mut app = Applicability::MachineApplicable; | ||
let slice_name = snippet_with_applicability(cx, recv.span, "..", &mut app); | ||
span_lint_and_sugg( | ||
cx, | ||
GET_FIRST, | ||
expr.span, | ||
&format!("accessing first element with `{0}.get(0)`", slice_name), | ||
"try", | ||
format!("{}.first()", slice_name), | ||
app, | ||
); | ||
} | ||
} | ||
} |
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