extract conditions for from_iter_instead_of_collect into its own module

This commit is contained in:
Takayuki Maeda 2021-03-17 13:22:25 +09:00
parent 62490c41af
commit d380769952
5 changed files with 14 additions and 18 deletions

View file

@ -1,6 +1,6 @@
use crate::utils::{method_chain_args, single_segment_path};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{method_chain_args, single_segment_path};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;

View file

@ -1,5 +1,5 @@
use crate::utils::method_chain_args;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::method_chain_args;
use clippy_utils::source::snippet_with_applicability;
use if_chain::if_chain;
use rustc_ast::ast;

View file

@ -1,20 +1,22 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::ty::implements_trait;
use clippy_utils::{get_trait_def_id, paths, sugg};
use clippy_utils::{get_trait_def_id, match_qpath, paths, sugg};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::ExprKind;
use rustc_lint::{LateContext, LintContext};
use rustc_middle::ty::Ty;
use rustc_span::sym;
use super::FROM_ITER_INSTEAD_OF_COLLECT;
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>], func_kind: &ExprKind<'_>) {
if_chain! {
if let hir::ExprKind::Path(path) = func_kind;
if match_qpath(path, &["from_iter"]);
let ty = cx.typeck_results().expr_ty(expr);
let arg_ty = cx.typeck_results().expr_ty(&args[0]);
if_chain! {
if let Some(from_iter_id) = get_trait_def_id(cx, &paths::FROM_ITERATOR);
if let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator);

View file

@ -63,8 +63,8 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{contains_ty, implements_trait, is_copy, is_type_diagnostic_item};
use clippy_utils::{
contains_return, get_trait_def_id, in_macro, iter_input_pats, match_def_path, match_qpath, method_calls,
method_chain_args, paths, return_ty, single_segment_path, SpanlessEq,
contains_return, get_trait_def_id, in_macro, iter_input_pats, match_qpath, method_calls, paths, return_ty,
SpanlessEq,
};
use if_chain::if_chain;
use rustc_ast::ast;
@ -1777,22 +1777,17 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
match expr.kind {
hir::ExprKind::Call(ref func, ref args) => {
if let hir::ExprKind::Path(path) = &func.kind {
if match_qpath(path, &["from_iter"]) {
from_iter_instead_of_collect::check(cx, expr, args);
}
}
from_iter_instead_of_collect::check(cx, expr, args, &func.kind);
},
hir::ExprKind::MethodCall(ref method_call, ref method_span, ref args, _) => {
or_fun_call::check(cx, expr, *method_span, &method_call.ident.as_str(), args);
expect_fun_call::check(cx, expr, *method_span, &method_call.ident.as_str(), args);
clone_on_copy::check(cx, expr, method_call.ident.name, args);
clone_on_ref_ptr::check(cx, expr, method_call.ident.name, args);
let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
inefficient_to_string::check(cx, expr, method_call.ident.name, args);
single_char_add_str::check(cx, expr, args);
let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
match self_ty.kind() {
ty::Ref(_, ty, _) if *ty.kind() == ty::Str => {
for &(method, pos) in &PATTERN_METHODS {

View file

@ -1,6 +1,5 @@
use crate::methods::{single_char_insert_string, single_char_push_string};
use crate::utils::match_def_path;
use crate::utils::paths;
use clippy_utils::{match_def_path, paths};
use rustc_hir as hir;
use rustc_lint::LateContext;