Skip to content

Commit 92612c9

Browse files
committed
Auto merge of rust-lang#3893 - mati865:rustup, r=oli-obk
Rustup Supersedes rust-lang#3889 Addresses some review comments from previous PR and rustups to rust-lang/rust#58899
2 parents 54e2051 + 6cb0605 commit 92612c9

22 files changed

+233
-152
lines changed

clippy_lints/src/attrs.rs

+83-77
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc::ty::{self, TyCtxt};
1414
use rustc::{declare_tool_lint, lint_array};
1515
use rustc_errors::Applicability;
1616
use semver::Version;
17-
use syntax::ast::{AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
17+
use syntax::ast::{AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
1818
use syntax::source_map::Span;
1919

2020
declare_clippy_lint! {
@@ -208,22 +208,24 @@ impl LintPass for AttrPass {
208208
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
209209
fn check_attribute(&mut self, cx: &LateContext<'a, 'tcx>, attr: &'tcx Attribute) {
210210
if let Some(items) = &attr.meta_item_list() {
211-
match &*attr.name().as_str() {
212-
"allow" | "warn" | "deny" | "forbid" => {
213-
check_clippy_lint_names(cx, items);
214-
},
215-
_ => {},
216-
}
217-
if items.is_empty() || attr.name() != "deprecated" {
218-
return;
219-
}
220-
for item in items {
221-
if_chain! {
222-
if let NestedMetaItemKind::MetaItem(mi) = &item.node;
223-
if let MetaItemKind::NameValue(lit) = &mi.node;
224-
if mi.name() == "since";
225-
then {
226-
check_semver(cx, item.span, lit);
211+
if let Some(ident) = attr.ident_str() {
212+
match ident {
213+
"allow" | "warn" | "deny" | "forbid" => {
214+
check_clippy_lint_names(cx, items);
215+
},
216+
_ => {},
217+
}
218+
if items.is_empty() || !attr.check_name("deprecated") {
219+
return;
220+
}
221+
for item in items {
222+
if_chain! {
223+
if let NestedMetaItem::MetaItem(mi) = &item;
224+
if let MetaItemKind::NameValue(lit) = &mi.node;
225+
if mi.check_name("since");
226+
then {
227+
check_semver(cx, item.span(), lit);
228+
}
227229
}
228230
}
229231
}
@@ -236,55 +238,57 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
236238
}
237239
match item.node {
238240
ItemKind::ExternCrate(..) | ItemKind::Use(..) => {
239-
let skip_unused_imports = item.attrs.iter().any(|attr| attr.name() == "macro_use");
241+
let skip_unused_imports = item.attrs.iter().any(|attr| attr.check_name("macro_use"));
240242

241243
for attr in &item.attrs {
242244
if let Some(lint_list) = &attr.meta_item_list() {
243-
match &*attr.name().as_str() {
244-
"allow" | "warn" | "deny" | "forbid" => {
245-
// whitelist `unused_imports` and `deprecated` for `use` items
246-
// and `unused_imports` for `extern crate` items with `macro_use`
247-
for lint in lint_list {
248-
match item.node {
249-
ItemKind::Use(..) => {
250-
if is_word(lint, "unused_imports") || is_word(lint, "deprecated") {
251-
return;
252-
}
253-
},
254-
ItemKind::ExternCrate(..) => {
255-
if is_word(lint, "unused_imports") && skip_unused_imports {
256-
return;
257-
}
258-
if is_word(lint, "unused_extern_crates") {
259-
return;
260-
}
261-
},
262-
_ => {},
263-
}
264-
}
265-
let line_span = last_line_of_span(cx, attr.span);
266-
267-
if let Some(mut sugg) = snippet_opt(cx, line_span) {
268-
if sugg.contains("#[") {
269-
span_lint_and_then(
270-
cx,
271-
USELESS_ATTRIBUTE,
272-
line_span,
273-
"useless lint attribute",
274-
|db| {
275-
sugg = sugg.replacen("#[", "#![", 1);
276-
db.span_suggestion(
277-
line_span,
278-
"if you just forgot a `!`, use",
279-
sugg,
280-
Applicability::MachineApplicable,
281-
);
245+
if let Some(ident) = attr.ident_str() {
246+
match ident {
247+
"allow" | "warn" | "deny" | "forbid" => {
248+
// whitelist `unused_imports` and `deprecated` for `use` items
249+
// and `unused_imports` for `extern crate` items with `macro_use`
250+
for lint in lint_list {
251+
match item.node {
252+
ItemKind::Use(..) => {
253+
if is_word(lint, "unused_imports") || is_word(lint, "deprecated") {
254+
return;
255+
}
256+
},
257+
ItemKind::ExternCrate(..) => {
258+
if is_word(lint, "unused_imports") && skip_unused_imports {
259+
return;
260+
}
261+
if is_word(lint, "unused_extern_crates") {
262+
return;
263+
}
282264
},
283-
);
265+
_ => {},
266+
}
284267
}
285-
}
286-
},
287-
_ => {},
268+
let line_span = last_line_of_span(cx, attr.span);
269+
270+
if let Some(mut sugg) = snippet_opt(cx, line_span) {
271+
if sugg.contains("#[") {
272+
span_lint_and_then(
273+
cx,
274+
USELESS_ATTRIBUTE,
275+
line_span,
276+
"useless lint attribute",
277+
|db| {
278+
sugg = sugg.replacen("#[", "#![", 1);
279+
db.span_suggestion(
280+
line_span,
281+
"if you just forgot a `!`, use",
282+
sugg,
283+
Applicability::MachineApplicable,
284+
);
285+
},
286+
);
287+
}
288+
}
289+
},
290+
_ => {},
291+
}
288292
}
289293
}
290294
}
@@ -311,10 +315,11 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
311315
let lint_store = cx.lints();
312316
for lint in items {
313317
if_chain! {
314-
if let Some(word) = lint.word();
315-
if let Some(tool_name) = word.is_scoped();
318+
if let Some(meta_item) = lint.meta_item();
319+
if meta_item.path.segments.len() > 1;
320+
if let tool_name = meta_item.path.segments[0].ident;
316321
if tool_name.as_str() == "clippy";
317-
let name = word.name();
322+
let name = meta_item.path.segments.last().unwrap().ident.name;
318323
if let CheckLintNameResult::Tool(Err((None, _))) = lint_store.check_lint_name(
319324
&name.as_str(),
320325
Some(tool_name.as_str()),
@@ -323,7 +328,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
323328
span_lint_and_then(
324329
cx,
325330
UNKNOWN_CLIPPY_LINTS,
326-
lint.span,
331+
lint.span(),
327332
&format!("unknown clippy lint: clippy::{}", name),
328333
|db| {
329334
if name.as_str().chars().any(char::is_uppercase) {
@@ -337,7 +342,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
337342
CheckLintNameResult::NoLint(None) => (),
338343
_ => {
339344
db.span_suggestion(
340-
lint.span,
345+
lint.span(),
341346
"lowercase the lint name",
342347
name_lower,
343348
Applicability::MaybeIncorrect,
@@ -352,22 +357,22 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
352357
}
353358
}
354359

355-
fn is_relevant_item(tcx: TyCtxt<'_, '_, '_>, item: &Item) -> bool {
360+
fn is_relevant_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &Item) -> bool {
356361
if let ItemKind::Fn(_, _, _, eid) = item.node {
357362
is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value)
358363
} else {
359364
true
360365
}
361366
}
362367

363-
fn is_relevant_impl(tcx: TyCtxt<'_, '_, '_>, item: &ImplItem) -> bool {
368+
fn is_relevant_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &ImplItem) -> bool {
364369
match item.node {
365370
ImplItemKind::Method(_, eid) => is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value),
366371
_ => false,
367372
}
368373
}
369374

370-
fn is_relevant_trait(tcx: TyCtxt<'_, '_, '_>, item: &TraitItem) -> bool {
375+
fn is_relevant_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &TraitItem) -> bool {
371376
match item.node {
372377
TraitItemKind::Method(_, TraitMethod::Required(_)) => true,
373378
TraitItemKind::Method(_, TraitMethod::Provided(eid)) => {
@@ -377,7 +382,7 @@ fn is_relevant_trait(tcx: TyCtxt<'_, '_, '_>, item: &TraitItem) -> bool {
377382
}
378383
}
379384

380-
fn is_relevant_block(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool {
385+
fn is_relevant_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool {
381386
if let Some(stmt) = block.stmts.first() {
382387
match &stmt.node {
383388
StmtKind::Local(_) => true,
@@ -389,7 +394,7 @@ fn is_relevant_block(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, blo
389394
}
390395
}
391396

392-
fn is_relevant_expr(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
397+
fn is_relevant_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
393398
match &expr.node {
394399
ExprKind::Block(block, _) => is_relevant_block(tcx, tables, block),
395400
ExprKind::Ret(Some(e)) => is_relevant_expr(tcx, tables, e),
@@ -443,7 +448,7 @@ fn check_attrs(cx: &LateContext<'_, '_>, span: Span, name: Name, attrs: &[Attrib
443448
}
444449

445450
if let Some(values) = attr.meta_item_list() {
446-
if values.len() != 1 || attr.name() != "inline" {
451+
if values.len() != 1 || !attr.check_name("inline") {
447452
continue;
448453
}
449454
if is_word(&values[0], "always") {
@@ -476,8 +481,8 @@ fn check_semver(cx: &LateContext<'_, '_>, span: Span, lit: &Lit) {
476481
}
477482

478483
fn is_word(nmi: &NestedMetaItem, expected: &str) -> bool {
479-
if let NestedMetaItemKind::MetaItem(mi) = &nmi.node {
480-
mi.is_word() && mi.name() == expected
484+
if let NestedMetaItem::MetaItem(mi) = &nmi {
485+
mi.is_word() && mi.check_name(expected)
481486
} else {
482487
false
483488
}
@@ -514,15 +519,16 @@ impl EarlyLintPass for CfgAttrPass {
514519
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
515520
if_chain! {
516521
// check cfg_attr
517-
if attr.name() == "cfg_attr";
522+
if attr.check_name("cfg_attr");
518523
if let Some(items) = attr.meta_item_list();
519524
if items.len() == 2;
520525
// check for `rustfmt`
521526
if let Some(feature_item) = items[0].meta_item();
522-
if feature_item.name() == "rustfmt";
527+
if feature_item.check_name("rustfmt");
523528
// check for `rustfmt_skip` and `rustfmt::skip`
524529
if let Some(skip_item) = &items[1].meta_item();
525-
if skip_item.name() == "rustfmt_skip" || skip_item.name() == "skip";
530+
if skip_item.check_name("rustfmt_skip") ||
531+
skip_item.path.segments.last().expect("empty path in attribute").ident.name == "skip";
526532
// Only lint outer attributes, because custom inner attributes are unstable
527533
// Tracking issue: https://github.com/rust-lang/rust/issues/54726
528534
if let AttrStyle::Outer = attr.style;

clippy_lints/src/consts.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Hash for Constant {
117117
}
118118

119119
impl Constant {
120-
pub fn partial_cmp(tcx: TyCtxt<'_, '_, '_>, cmp_type: ty::Ty<'_>, left: &Self, right: &Self) -> Option<Ordering> {
120+
pub fn partial_cmp(tcx: TyCtxt<'_, '_, '_>, cmp_type: Ty<'_>, left: &Self, right: &Self) -> Option<Ordering> {
121121
match (left, right) {
122122
(&Constant::Str(ref ls), &Constant::Str(ref rs)) => Some(ls.cmp(rs)),
123123
(&Constant::Char(ref l), &Constant::Char(ref r)) => Some(l.cmp(r)),
@@ -268,7 +268,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
268268
}
269269

270270
#[allow(clippy::cast_possible_wrap)]
271-
fn constant_not(&self, o: &Constant, ty: ty::Ty<'_>) -> Option<Constant> {
271+
fn constant_not(&self, o: &Constant, ty: Ty<'_>) -> Option<Constant> {
272272
use self::Constant::*;
273273
match *o {
274274
Bool(b) => Some(Bool(!b)),
@@ -284,7 +284,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
284284
}
285285
}
286286

287-
fn constant_negate(&self, o: &Constant, ty: ty::Ty<'_>) -> Option<Constant> {
287+
fn constant_negate(&self, o: &Constant, ty: Ty<'_>) -> Option<Constant> {
288288
use self::Constant::*;
289289
match *o {
290290
Int(value) => {

clippy_lints/src/doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub fn check_attrs<'a>(cx: &EarlyContext<'_>, valid_idents: &FxHashSet<String>,
152152
spans.extend_from_slice(&current_spans);
153153
doc.push_str(&current);
154154
}
155-
} else if attr.name() == "doc" {
155+
} else if attr.check_name("doc") {
156156
// ignore mix of sugared and non-sugared doc
157157
return;
158158
}

clippy_lints/src/eta_reduction.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use if_chain::if_chain;
22
use rustc::hir::*;
33
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
4-
use rustc::ty;
4+
use rustc::ty::{self, Ty};
55
use rustc::{declare_tool_lint, lint_array};
66
use rustc_errors::Applicability;
77

@@ -129,54 +129,54 @@ fn get_ufcs_type_name(
129129
method_def_id: def_id::DefId,
130130
self_arg: &Expr,
131131
) -> std::option::Option<String> {
132-
let expected_type_of_self = &cx.tcx.fn_sig(method_def_id).inputs_and_output().skip_binder()[0].sty;
133-
let actual_type_of_self = &cx.tables.node_type(self_arg.hir_id).sty;
132+
let expected_type_of_self = &cx.tcx.fn_sig(method_def_id).inputs_and_output().skip_binder()[0];
133+
let actual_type_of_self = &cx.tables.node_type(self_arg.hir_id);
134134

135135
if let Some(trait_id) = cx.tcx.trait_of_item(method_def_id) {
136-
if match_borrow_depth(expected_type_of_self, actual_type_of_self) {
137-
return Some(cx.tcx.item_path_str(trait_id));
136+
if match_borrow_depth(expected_type_of_self, &actual_type_of_self) {
137+
return Some(cx.tcx.def_path_str(trait_id));
138138
}
139139
}
140140

141141
cx.tcx.impl_of_method(method_def_id).and_then(|_| {
142142
//a type may implicitly implement other type's methods (e.g. Deref)
143-
if match_types(expected_type_of_self, actual_type_of_self) {
143+
if match_types(expected_type_of_self, &actual_type_of_self) {
144144
return Some(get_type_name(cx, &actual_type_of_self));
145145
}
146146
None
147147
})
148148
}
149149

150-
fn match_borrow_depth(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool {
151-
match (lhs, rhs) {
152-
(ty::Ref(_, t1, _), ty::Ref(_, t2, _)) => match_borrow_depth(&t1.sty, &t2.sty),
150+
fn match_borrow_depth(lhs: Ty<'_>, rhs: Ty<'_>) -> bool {
151+
match (&lhs.sty, &rhs.sty) {
152+
(ty::Ref(_, t1, _), ty::Ref(_, t2, _)) => match_borrow_depth(&t1, &t2),
153153
(l, r) => match (l, r) {
154154
(ty::Ref(_, _, _), _) | (_, ty::Ref(_, _, _)) => false,
155155
(_, _) => true,
156156
},
157157
}
158158
}
159159

160-
fn match_types(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool {
161-
match (lhs, rhs) {
160+
fn match_types(lhs: Ty<'_>, rhs: Ty<'_>) -> bool {
161+
match (&lhs.sty, &rhs.sty) {
162162
(ty::Bool, ty::Bool)
163163
| (ty::Char, ty::Char)
164164
| (ty::Int(_), ty::Int(_))
165165
| (ty::Uint(_), ty::Uint(_))
166166
| (ty::Str, ty::Str) => true,
167167
(ty::Ref(_, t1, _), ty::Ref(_, t2, _))
168168
| (ty::Array(t1, _), ty::Array(t2, _))
169-
| (ty::Slice(t1), ty::Slice(t2)) => match_types(&t1.sty, &t2.sty),
169+
| (ty::Slice(t1), ty::Slice(t2)) => match_types(t1, t2),
170170
(ty::Adt(def1, _), ty::Adt(def2, _)) => def1 == def2,
171171
(_, _) => false,
172172
}
173173
}
174174

175-
fn get_type_name(cx: &LateContext<'_, '_>, kind: &ty::TyKind<'_>) -> String {
176-
match kind {
177-
ty::Adt(t, _) => cx.tcx.item_path_str(t.did),
178-
ty::Ref(_, r, _) => get_type_name(cx, &r.sty),
179-
_ => kind.to_string(),
175+
fn get_type_name(cx: &LateContext<'_, '_>, ty: Ty<'_>) -> String {
176+
match ty.sty {
177+
ty::Adt(t, _) => cx.tcx.def_path_str(t.did),
178+
ty::Ref(_, r, _) => get_type_name(cx, &r),
179+
_ => ty.to_string(),
180180
}
181181
}
182182

0 commit comments

Comments
 (0)