Skip to content

Commit 9ceece9

Browse files
committed
Make it compile and make ui/collect.rs tests pass again
1 parent 1ff2f3f commit 9ceece9

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

clippy_lints/src/collect.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
use itertools::{repeat_n, Itertools};
22
use rustc::hir::*;
3-
use rustc::lint::*;
4-
use rustc::ty::{AssociatedKind, TypeVariants};
3+
use rustc::ty::{AssociatedKind, TyKind};
54
use syntax::ast::NodeId;
65

76
use std::collections::HashSet;
87

8+
use crate::rustc_errors::Applicability;
9+
use crate::rustc::lint::{
10+
LateContext, LateLintPass, LintArray, LintPass,
11+
};
12+
use crate::rustc::{declare_tool_lint, lint_array};
913
use crate::utils::{match_trait_method, match_type, span_lint_and_sugg};
1014
use crate::utils::paths;
1115

16+
use if_chain::if_chain;
17+
1218
/// **What it does:** Detects collect calls on iterators to collections
1319
/// of either `Result<_, E>` or `Option<_>` inside functions that also
1420
/// have such a return type.
@@ -69,11 +75,11 @@ struct Suggestion {
6975

7076
fn format_suggestion_pattern<'a, 'tcx>(
7177
cx: &LateContext<'a, 'tcx>,
72-
collection_ty: &TypeVariants,
78+
collection_ty: &TyKind<'_>,
7379
is_option: bool,
7480
) -> String {
7581
let collection_pat = match collection_ty {
76-
TypeVariants::TyAdt(def, subs) => {
82+
TyKind::Adt(def, subs) => {
7783
let mut buf = cx.tcx.item_path_str(def.did);
7884

7985
if !subs.is_empty() {
@@ -84,7 +90,7 @@ fn format_suggestion_pattern<'a, 'tcx>(
8490

8591
buf
8692
},
87-
TypeVariants::TyParam(p) => p.to_string(),
93+
TyKind::Param(p) => p.to_string(),
8894
_ => "_".into(),
8995
};
9096

@@ -96,8 +102,8 @@ fn format_suggestion_pattern<'a, 'tcx>(
96102
}
97103

98104
fn check_expr_for_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<Suggestion> {
99-
if let ExprMethodCall(ref method, _, ref args) = expr.node {
100-
if args.len() == 1 && method.name == "collect" && match_trait_method(cx, expr, &paths::ITERATOR) {
105+
if let ExprKind::MethodCall(ref method, _, ref args) = expr.node {
106+
if args.len() == 1 && method.ident.name == "collect" && match_trait_method(cx, expr, &paths::ITERATOR) {
101107
let collect_ty = cx.tables.expr_ty(expr);
102108

103109
if match_type(cx, collect_ty, &paths::OPTION) || match_type(cx, collect_ty, &paths::RESULT) {
@@ -153,7 +159,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
153159
}
154160

155161
if let Some(suggestion) = check_expr_for_collect(cx, expr) {
156-
let sugg_span = if let ExprMethodCall(_, call_span, _) = expr.node {
162+
let sugg_span = if let ExprKind::MethodCall(_, call_span, _) = expr.node {
157163
expr.span.between(call_span)
158164
} else {
159165
unreachable!()
@@ -169,14 +175,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
169175
suggestion.success_variant
170176
),
171177
format!("collect::<{}>()", suggestion.pattern),
178+
Applicability::MaybeIncorrect
172179
);
173180
}
174181
}
175182

176183
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt) {
177184
if_chain! {
178-
if let StmtDecl(ref decl, _) = stmt.node;
179-
if let DeclLocal(ref local) = decl.node;
185+
if let StmtKind::Decl(ref decl, _) = stmt.node;
186+
if let DeclKind::Local(ref local) = decl.node;
180187
if let Some(ref ty) = local.ty;
181188
if let Some(ref expr) = local.init;
182189
then {
@@ -192,7 +199,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
192199
"if you are only interested in the case where all values are `{}`, try",
193200
suggestion.success_variant
194201
),
195-
suggestion.pattern
202+
suggestion.pattern,
203+
Applicability::MaybeIncorrect
196204
);
197205
}
198206
}

tests/ui/collect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#![warn(possible_shortcircuiting_collect)]
1+
#![warn(clippy::possible_shortcircuiting_collect)]
22

33
use std::iter::FromIterator;
44

55
pub fn div(a: i32, b: &[i32]) -> Result<Vec<i32>, String> {
6-
let option_vec: Vec<_> = b.into_iter()
6+
let option_vec: Vec<_> = b.iter()
77
.cloned()
88
.map(|i| if i != 0 {
99
Ok(a / i)

0 commit comments

Comments
 (0)