Skip to content

Commit ce14897

Browse files
committed
Update Clippy, RLS and Rustfmt
1 parent 454f62b commit ce14897

File tree

128 files changed

+1861
-1226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+1861
-1226
lines changed

clippy_dev/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub struct FileChange {
191191
pub new_lines: String,
192192
}
193193

194-
/// Replace a region in a file delimited by two lines matching regexes.
194+
/// Replaces a region in a file delimited by two lines matching regexes.
195195
///
196196
/// `path` is the relative path to the file on which you want to perform the replacement.
197197
///
@@ -225,7 +225,7 @@ where
225225
file_change
226226
}
227227

228-
/// Replace a region in a text delimited by two lines matching regexes.
228+
/// Replaces a region in a text delimited by two lines matching regexes.
229229
///
230230
/// * `text` is the input text on which you want to perform the replacement
231231
/// * `start` is a `&str` that describes the delimiter line before the region you want to replace.

clippy_lints/src/approx_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn check_known_consts(cx: &LateContext<'_, '_>, e: &Expr, s: symbol::Symbol, mod
104104
}
105105
}
106106

107-
/// Returns false if the number of significant figures in `value` are
107+
/// Returns `false` if the number of significant figures in `value` are
108108
/// less than `min_digits`; otherwise, returns true if `value` is equal
109109
/// to `constant`, rounded to the number of digits present in `value`.
110110
fn is_approx_const(constant: f64, value: &str, min_digits: usize) -> bool {

clippy_lints/src/assertions_on_constants.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1+
use if_chain::if_chain;
2+
use rustc::hir::{Expr, ExprKind};
3+
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4+
use rustc::{declare_tool_lint, lint_array};
5+
16
use crate::consts::{constant, Constant};
2-
use crate::rustc::hir::{Expr, ExprKind};
3-
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4-
use crate::rustc::{declare_tool_lint, lint_array};
57
use crate::syntax::ast::LitKind;
68
use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
7-
use if_chain::if_chain;
89

910
declare_clippy_lint! {
10-
/// **What it does:** Check to call assert!(true/false)
11+
/// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
1112
///
1213
/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
1314
/// panic!() or unreachable!()
1415
///
1516
/// **Known problems:** None
1617
///
1718
/// **Example:**
18-
/// ```no_run
19-
/// assert!(false);
19+
/// ```rust,ignore
20+
/// assert!(false)
2021
/// // or
21-
/// assert!(true);
22+
/// assert!(true)
2223
/// // or
2324
/// const B: bool = false;
24-
/// assert!(B);
25+
/// assert!(B)
2526
/// ```
2627
pub ASSERTIONS_ON_CONSTANTS,
2728
style,
28-
"assert!(true/false) will be optimized out by the compiler/should probably be replaced by a panic!() or unreachable!()"
29+
"`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
2930
}
3031

3132
pub struct AssertionsOnConstants;

clippy_lints/src/assign_ops.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
use crate::utils::{
2-
get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method, SpanlessEq,
3-
};
4-
use crate::utils::{higher, sugg};
51
use if_chain::if_chain;
62
use rustc::hir;
73
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
84
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
95
use rustc::{declare_tool_lint, lint_array};
106
use rustc_errors::Applicability;
117

8+
use crate::utils::{
9+
get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method, SpanlessEq,
10+
};
11+
use crate::utils::{higher, sugg};
12+
1213
declare_clippy_lint! {
1314
/// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
1415
/// patterns.
@@ -19,9 +20,10 @@ declare_clippy_lint! {
1920
/// implementations that differ from the regular `Op` impl.
2021
///
2122
/// **Example:**
22-
/// ```ignore
23+
/// ```rust
2324
/// let mut a = 5;
24-
/// ...
25+
/// let b = 0;
26+
/// // ...
2527
/// a = a + b;
2628
/// ```
2729
pub ASSIGN_OP_PATTERN,
@@ -36,12 +38,12 @@ declare_clippy_lint! {
3638
/// op= b`.
3739
///
3840
/// **Known problems:** Clippy cannot know for sure if `a op= a op b` should have
39-
/// been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore it suggests both.
41+
/// been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore, it suggests both.
4042
/// If `a op= a op b` is really the correct behaviour it should be
4143
/// written as `a = a op a op b` as it's less confusing.
4244
///
4345
/// **Example:**
44-
/// ```ignore
46+
/// ```rust
4547
/// let mut a = 5;
4648
/// ...
4749
/// a += a + b;

clippy_lints/src/attrs.rs

Lines changed: 83 additions & 77 deletions
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;

0 commit comments

Comments
 (0)