forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#8989 - kyoto7250:default_iter_empty, r=Alexendoo
feat(lint): add default_iter_empty close rust-lang#8915 This PR adds `default_iter_empty` lint. This lint checks `std::iter::Empty::default()` and replace with `std::iter::empty()`. Thank you in advance. --- changelog: add `default_instead_of_iter_empty` lint.
- Loading branch information
Showing
10 changed files
with
139 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::last_path_segment; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::{match_def_path, paths}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{def, Expr, ExprKind, GenericArg, QPath, TyKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// It checks for `std::iter::Empty::default()` and suggests replacing it with | ||
/// `std::iter::empty()`. | ||
/// ### Why is this bad? | ||
/// `std::iter::empty()` is the more idiomatic way. | ||
/// ### Example | ||
/// ```rust | ||
/// let _ = std::iter::Empty::<usize>::default(); | ||
/// let iter: std::iter::Empty<usize> = std::iter::Empty::default(); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// let _ = std::iter::empty::<usize>(); | ||
/// let iter: std::iter::Empty<usize> = std::iter::empty(); | ||
/// ``` | ||
#[clippy::version = "1.63.0"] | ||
pub DEFAULT_INSTEAD_OF_ITER_EMPTY, | ||
style, | ||
"check `std::iter::Empty::default()` and replace with `std::iter::empty()`" | ||
} | ||
declare_lint_pass!(DefaultIterEmpty => [DEFAULT_INSTEAD_OF_ITER_EMPTY]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for DefaultIterEmpty { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
if let ExprKind::Call(iter_expr, []) = &expr.kind | ||
&& let ExprKind::Path(QPath::TypeRelative(ty, _)) = &iter_expr.kind | ||
&& let TyKind::Path(ty_path) = &ty.kind | ||
&& let QPath::Resolved(None, path) = ty_path | ||
&& let def::Res::Def(_, def_id) = &path.res | ||
&& match_def_path(cx, *def_id, &paths::ITER_EMPTY) | ||
{ | ||
let mut applicability = Applicability::MachineApplicable; | ||
let sugg = make_sugg(cx, ty_path, &mut applicability); | ||
span_lint_and_sugg( | ||
cx, | ||
DEFAULT_INSTEAD_OF_ITER_EMPTY, | ||
expr.span, | ||
"`std::iter::empty()` is the more idiomatic way", | ||
"try", | ||
sugg, | ||
applicability, | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn make_sugg(cx: &LateContext<'_>, ty_path: &rustc_hir::QPath<'_>, applicability: &mut Applicability) -> String { | ||
if let Some(last) = last_path_segment(ty_path).args | ||
&& let Some(iter_ty) = last.args.iter().find_map(|arg| match arg { | ||
GenericArg::Type(ty) => Some(ty), | ||
_ => None, | ||
}) | ||
{ | ||
format!("std::iter::empty::<{}>()", snippet_with_applicability(cx, iter_ty.span, "..", applicability)) | ||
} else { | ||
"std::iter::empty()".to_owned() | ||
} | ||
} |
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
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,21 @@ | ||
// run-rustfix | ||
#![warn(clippy::default_instead_of_iter_empty)] | ||
#![allow(dead_code)] | ||
use std::collections::HashMap; | ||
|
||
#[derive(Default)] | ||
struct Iter { | ||
iter: std::iter::Empty<usize>, | ||
} | ||
|
||
fn main() { | ||
// Do lint. | ||
let _ = std::iter::empty::<usize>(); | ||
let _ = std::iter::empty::<HashMap<usize, usize>>(); | ||
let _foo: std::iter::Empty<usize> = std::iter::empty(); | ||
|
||
// Do not lint. | ||
let _ = Vec::<usize>::default(); | ||
let _ = String::default(); | ||
let _ = Iter::default(); | ||
} |
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,21 @@ | ||
// run-rustfix | ||
#![warn(clippy::default_instead_of_iter_empty)] | ||
#![allow(dead_code)] | ||
use std::collections::HashMap; | ||
|
||
#[derive(Default)] | ||
struct Iter { | ||
iter: std::iter::Empty<usize>, | ||
} | ||
|
||
fn main() { | ||
// Do lint. | ||
let _ = std::iter::Empty::<usize>::default(); | ||
let _ = std::iter::Empty::<HashMap<usize, usize>>::default(); | ||
let _foo: std::iter::Empty<usize> = std::iter::Empty::default(); | ||
|
||
// Do not lint. | ||
let _ = Vec::<usize>::default(); | ||
let _ = String::default(); | ||
let _ = Iter::default(); | ||
} |
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,22 @@ | ||
error: `std::iter::empty()` is the more idiomatic way | ||
--> $DIR/default_instead_of_iter_empty.rs:13:13 | ||
| | ||
LL | let _ = std::iter::Empty::<usize>::default(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::iter::empty::<usize>()` | ||
| | ||
= note: `-D clippy::default-instead-of-iter-empty` implied by `-D warnings` | ||
|
||
error: `std::iter::empty()` is the more idiomatic way | ||
--> $DIR/default_instead_of_iter_empty.rs:14:13 | ||
| | ||
LL | let _ = std::iter::Empty::<HashMap<usize, usize>>::default(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::iter::empty::<HashMap<usize, usize>>()` | ||
|
||
error: `std::iter::empty()` is the more idiomatic way | ||
--> $DIR/default_instead_of_iter_empty.rs:15:41 | ||
| | ||
LL | let _foo: std::iter::Empty<usize> = std::iter::Empty::default(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::iter::empty()` | ||
|
||
error: aborting due to 3 previous errors | ||
|