From 784dd22aac3f58eebc73ff54ae0ea43682392e68 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Sat, 25 Jul 2020 15:15:12 -0500 Subject: [PATCH 1/9] add `unsigned_abs` to signed integers --- library/core/src/num/mod.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 048c9c5ddaaee..eb50dc28b9f11 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -1601,6 +1601,29 @@ $EndFeature, " } } + doc_comment! { + concat!("Computes the absolute value of `self` without any wrapping +or panicking. + + +# Examples + +Basic usage: + +``` +", $Feature, "#![feature(unsigned_abs)] +assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), "); +assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), "); +assert_eq!((-128i8).unsigned_abs(), 128u8);", +$EndFeature, " +```"), + #[unstable(feature = "unsigned_abs", issue = "74913")] + #[inline] + pub const fn unsigned_abs(self) -> $UnsignedT { + self.wrapping_abs() as $UnsignedT + } + } + doc_comment! { concat!("Wrapping (modular) exponentiation. Computes `self.pow(exp)`, wrapping around at the boundary of the type. From 35d6a2ef2ba71d5ea120c2879825968babcf9a89 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 2 Aug 2020 16:30:09 +0200 Subject: [PATCH 2/9] Lint path statements to use drop for drop types --- src/librustc_lint/unused.rs | 20 ++++++++++++++++++-- src/test/ui/warn-path-statement.rs | 13 ++++++++++++- src/test/ui/warn-path-statement.stderr | 16 ++++++++++++++-- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 8d8fb8c3c6098..ee4361ce16b62 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -275,10 +275,26 @@ declare_lint_pass!(PathStatements => [PATH_STATEMENTS]); impl<'tcx> LateLintPass<'tcx> for PathStatements { fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) { - if let hir::StmtKind::Semi(ref expr) = s.kind { + if let hir::StmtKind::Semi(expr) = s.kind { if let hir::ExprKind::Path(_) = expr.kind { cx.struct_span_lint(PATH_STATEMENTS, s.span, |lint| { - lint.build("path statement with no effect").emit() + let ty = cx.typeck_results().expr_ty(expr); + if ty.needs_drop(cx.tcx, cx.param_env) { + let mut lint = lint.build("path statement drops value"); + if let Ok(snippet) = cx.sess().source_map().span_to_snippet(expr.span) { + lint.span_suggestion( + s.span, + "use `drop` to clarify the intent", + format!("drop({});", snippet), + Applicability::MachineApplicable, + ); + } else { + lint.span_help(s.span, "use `drop` to clarify the intent"); + } + lint.emit() + } else { + lint.build("path statement with no effect").emit() + } }); } } diff --git a/src/test/ui/warn-path-statement.rs b/src/test/ui/warn-path-statement.rs index e8525f8b892d6..2435be623f310 100644 --- a/src/test/ui/warn-path-statement.rs +++ b/src/test/ui/warn-path-statement.rs @@ -1,6 +1,17 @@ // compile-flags: -D path-statements -fn main() { +struct Droppy; + +impl Drop for Droppy { + fn drop(&mut self) {} +} +fn main() { let x = 10; x; //~ ERROR path statement with no effect + + let y = Droppy; + y; //~ ERROR path statement drops value + + let z = (Droppy,); + z; //~ ERROR path statement drops value } diff --git a/src/test/ui/warn-path-statement.stderr b/src/test/ui/warn-path-statement.stderr index 30afb99e5f02c..248d2ef299be3 100644 --- a/src/test/ui/warn-path-statement.stderr +++ b/src/test/ui/warn-path-statement.stderr @@ -1,10 +1,22 @@ error: path statement with no effect - --> $DIR/warn-path-statement.rs:5:5 + --> $DIR/warn-path-statement.rs:10:5 | LL | x; | ^^ | = note: requested on the command line with `-D path-statements` -error: aborting due to previous error +error: path statement drops value + --> $DIR/warn-path-statement.rs:13:5 + | +LL | y; + | ^^ help: use `drop` to clarify the intent: `drop(y);` + +error: path statement drops value + --> $DIR/warn-path-statement.rs:16:5 + | +LL | z; + | ^^ help: use `drop` to clarify the intent: `drop(z);` + +error: aborting due to 3 previous errors From e97e6fbe343d0afac795e6c13201ce31047dc219 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 2 Aug 2020 22:24:54 -0400 Subject: [PATCH 3/9] Fix logging for rustdoc --- src/librustdoc/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 002c5f967105d..4f08452767a87 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -43,7 +43,7 @@ extern crate rustc_trait_selection; extern crate rustc_typeck; extern crate test as testing; #[macro_use] -extern crate log; +extern crate tracing as log; use std::default::Default; use std::env; From 2e5c50195aa0345e174ff9970ec58b7e154c0132 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Mon, 3 Aug 2020 12:18:10 +0900 Subject: [PATCH 4/9] Do not trigger `unused_braces` for `while let` --- src/librustc_lint/unused.rs | 5 ++++- .../issue-74883-unused-paren-baren-yield.rs | 11 ++--------- ...issue-74883-unused-paren-baren-yield.stderr | 18 +++--------------- ...used-braces-while-let-with-mutable-value.rs | 12 ++++++++++++ 4 files changed, 21 insertions(+), 25 deletions(-) create mode 100644 src/test/ui/lint/unused-braces-while-let-with-mutable-value.rs diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index a33f920603592..6a433f4e7c42b 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -520,7 +520,10 @@ trait UnusedDelimLint { (cond, UnusedDelimsCtx::IfCond, true, Some(left), Some(right)) } - While(ref cond, ref block, ..) => { + // Do not lint `unused_braces` in `while let` expressions. + While(ref cond, ref block, ..) + if !matches!(cond.kind, Let(_, _)) || Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => + { let left = e.span.lo() + rustc_span::BytePos(5); let right = block.span.lo(); (cond, UnusedDelimsCtx::WhileCond, true, Some(left), Some(right)) diff --git a/src/test/ui/lint/issue-74883-unused-paren-baren-yield.rs b/src/test/ui/lint/issue-74883-unused-paren-baren-yield.rs index 02182ec299321..8064c3a88d1d9 100644 --- a/src/test/ui/lint/issue-74883-unused-paren-baren-yield.rs +++ b/src/test/ui/lint/issue-74883-unused-paren-baren-yield.rs @@ -15,15 +15,8 @@ fn main() { while let Some(_) = ((yield)) {} //~ ERROR: unnecessary parentheses {{yield}}; //~ ERROR: unnecessary braces {( yield )}; //~ ERROR: unnecessary parentheses - - // FIXME: Reduce duplicate warnings. - // Perhaps we should tweak checks in `BlockRetValue`? - while let Some(_) = {(yield)} {} - //~^ ERROR: unnecessary braces - //~| ERROR: unnecessary parentheses - while let Some(_) = {{yield}} {} - //~^ ERROR: unnecessary braces - //~| ERROR: unnecessary braces + while let Some(_) = {(yield)} {} //~ ERROR: unnecessary parentheses + while let Some(_) = {{yield}} {} //~ ERROR: unnecessary braces // FIXME: It'd be great if we could also warn them. ((yield)); diff --git a/src/test/ui/lint/issue-74883-unused-paren-baren-yield.stderr b/src/test/ui/lint/issue-74883-unused-paren-baren-yield.stderr index 267cc9e031a11..3f6260dc6e19e 100644 --- a/src/test/ui/lint/issue-74883-unused-paren-baren-yield.stderr +++ b/src/test/ui/lint/issue-74883-unused-paren-baren-yield.stderr @@ -34,29 +34,17 @@ error: unnecessary parentheses around block return value LL | {( yield )}; | ^^^^^^^^^ help: remove these parentheses -error: unnecessary braces around `let` scrutinee expression - --> $DIR/issue-74883-unused-paren-baren-yield.rs:21:29 - | -LL | while let Some(_) = {(yield)} {} - | ^^^^^^^^^ help: remove these braces - error: unnecessary parentheses around block return value - --> $DIR/issue-74883-unused-paren-baren-yield.rs:21:30 + --> $DIR/issue-74883-unused-paren-baren-yield.rs:18:30 | LL | while let Some(_) = {(yield)} {} | ^^^^^^^ help: remove these parentheses -error: unnecessary braces around `let` scrutinee expression - --> $DIR/issue-74883-unused-paren-baren-yield.rs:24:29 - | -LL | while let Some(_) = {{yield}} {} - | ^^^^^^^^^ help: remove these braces - error: unnecessary braces around block return value - --> $DIR/issue-74883-unused-paren-baren-yield.rs:24:30 + --> $DIR/issue-74883-unused-paren-baren-yield.rs:19:30 | LL | while let Some(_) = {{yield}} {} | ^^^^^^^ help: remove these braces -error: aborting due to 8 previous errors +error: aborting due to 6 previous errors diff --git a/src/test/ui/lint/unused-braces-while-let-with-mutable-value.rs b/src/test/ui/lint/unused-braces-while-let-with-mutable-value.rs new file mode 100644 index 0000000000000..ac547293c583a --- /dev/null +++ b/src/test/ui/lint/unused-braces-while-let-with-mutable-value.rs @@ -0,0 +1,12 @@ +// check-pass + +#![deny(unused_braces)] + +fn main() { + let mut a = Some(3); + // Shouldn't warn below `a`. + while let Some(ref mut v) = {a} { + a.as_mut().map(|a| std::mem::swap(a, v)); + break; + } +} From f130e18c01a0108af23710418d8fb9eddb7647e1 Mon Sep 17 00:00:00 2001 From: David Sonder Date: Mon, 3 Aug 2020 16:02:20 +0200 Subject: [PATCH 5/9] Enable docs on in the x86_64-unknown-linux-musl manifest Add the rust-docs component to toolchain x86_64-unknown-linux-musl, which allows people using rustup on their musl-based linux distribution to download the rust-docs. Generating and uploading the docs was enabled in b5d143b. --- src/tools/build-manifest/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tools/build-manifest/src/main.rs b/src/tools/build-manifest/src/main.rs index 6ac89f2b1b82a..5dede95a85823 100644 --- a/src/tools/build-manifest/src/main.rs +++ b/src/tools/build-manifest/src/main.rs @@ -156,6 +156,7 @@ static DOCS_TARGETS: &[&str] = &[ "x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", + "x86_64-unknown-linux-musl", ]; static MINGW: &[&str] = &["i686-pc-windows-gnu", "x86_64-pc-windows-gnu"]; From d2fc809fdb2e92581a0ecd70dec3e179dbd3439a Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Mon, 3 Aug 2020 10:11:30 -0400 Subject: [PATCH 6/9] Disable building rust-analyzer on riscv64 riscv64 has an LLVM bug that makes rust-analyzer not build. --- src/bootstrap/dist.rs | 74 +++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index a4a1d5193b9b9..98b6be29c073b 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -1355,7 +1355,7 @@ pub struct RustAnalyzer { } impl Step for RustAnalyzer { - type Output = PathBuf; + type Output = Option; const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -1373,11 +1373,17 @@ impl Step for RustAnalyzer { }); } - fn run(self, builder: &Builder<'_>) -> PathBuf { + fn run(self, builder: &Builder<'_>) -> Option { let compiler = self.compiler; let target = self.target; assert!(builder.config.extended); + if target.contains("riscv64") { + // riscv64 currently has an LLVM bug that makes rust-analyzer unable + // to build. See #74813 for details. + return None; + } + let src = builder.src.join("src/tools/rust-analyzer"); let release_num = builder.release_num("rust-analyzer/crates/rust-analyzer"); let name = pkgname(builder, "rust-analyzer"); @@ -1431,7 +1437,7 @@ impl Step for RustAnalyzer { builder.info(&format!("Dist rust-analyzer stage{} ({})", compiler.stage, target)); let _time = timeit(builder); builder.run(&mut cmd); - distdir(builder).join(format!("{}-{}.tar.gz", name, target.triple)) + Some(distdir(builder).join(format!("{}-{}.tar.gz", name, target.triple))) } } @@ -1789,7 +1795,7 @@ impl Step for Extended { tarballs.push(rustc_installer); tarballs.push(cargo_installer); tarballs.extend(rls_installer.clone()); - tarballs.push(rust_analyzer_installer.clone()); + tarballs.extend(rust_analyzer_installer.clone()); tarballs.push(clippy_installer); tarballs.extend(miri_installer.clone()); tarballs.extend(rustfmt_installer.clone()); @@ -1867,7 +1873,9 @@ impl Step for Extended { if rls_installer.is_none() { contents = filter(&contents, "rls"); } - contents = filter(&contents, "rust-analyzer"); + if rust_analyzer_installer.is_none() { + contents = filter(&contents, "rust-analyzer"); + } if miri_installer.is_none() { contents = filter(&contents, "miri"); } @@ -1914,7 +1922,9 @@ impl Step for Extended { if rls_installer.is_some() { prepare("rls"); } - prepare("rust-analyzer"); + if rust_analyzer_installer.is_some() { + prepare("rust-analyzer"); + } if miri_installer.is_some() { prepare("miri"); } @@ -1976,7 +1986,9 @@ impl Step for Extended { if rls_installer.is_some() { prepare("rls"); } - prepare("rust-analyzer"); + if rust_analyzer_installer.is_some() { + prepare("rust-analyzer"); + } if miri_installer.is_some() { prepare("miri"); } @@ -2076,23 +2088,25 @@ impl Step for Extended { .arg(etc.join("msi/remove-duplicates.xsl")), ); } - builder.run( - Command::new(&heat) - .current_dir(&exe) - .arg("dir") - .arg("rust-analyzer") - .args(&heat_flags) - .arg("-cg") - .arg("RustAnalyzerGroup") - .arg("-dr") - .arg("RustAnalyzer") - .arg("-var") - .arg("var.RustAnalyzerDir") - .arg("-out") - .arg(exe.join("RustAnalyzerGroup.wxs")) - .arg("-t") - .arg(etc.join("msi/remove-duplicates.xsl")), - ); + if rust_analyzer_installer.is_some() { + builder.run( + Command::new(&heat) + .current_dir(&exe) + .arg("dir") + .arg("rust-analyzer") + .args(&heat_flags) + .arg("-cg") + .arg("RustAnalyzerGroup") + .arg("-dr") + .arg("RustAnalyzer") + .arg("-var") + .arg("var.RustAnalyzerDir") + .arg("-out") + .arg(exe.join("RustAnalyzerGroup.wxs")) + .arg("-t") + .arg(etc.join("msi/remove-duplicates.xsl")), + ); + } builder.run( Command::new(&heat) .current_dir(&exe) @@ -2186,7 +2200,9 @@ impl Step for Extended { if rls_installer.is_some() { cmd.arg("-dRlsDir=rls"); } - cmd.arg("-dRustAnalyzerDir=rust-analyzer"); + if rust_analyzer_installer.is_some() { + cmd.arg("-dRustAnalyzerDir=rust-analyzer"); + } if miri_installer.is_some() { cmd.arg("-dMiriDir=miri"); } @@ -2206,7 +2222,9 @@ impl Step for Extended { if rls_installer.is_some() { candle("RlsGroup.wxs".as_ref()); } - candle("RustAnalyzerGroup.wxs".as_ref()); + if rust_analyzer_installer.is_some() { + candle("RustAnalyzerGroup.wxs".as_ref()); + } if miri_installer.is_some() { candle("MiriGroup.wxs".as_ref()); } @@ -2244,7 +2262,9 @@ impl Step for Extended { if rls_installer.is_some() { cmd.arg("RlsGroup.wixobj"); } - cmd.arg("RustAnalyzerGroup.wixobj"); + if rust_analyzer_installer.is_some() { + cmd.arg("RustAnalyzerGroup.wixobj"); + } if miri_installer.is_some() { cmd.arg("MiriGroup.wixobj"); } From 6deda6a6a05e6e6ace8fb015d610c6355efb0fd7 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 2 Aug 2020 23:53:41 -0400 Subject: [PATCH 7/9] Stabilize Ident::new_raw Tracking issue: #54723 This is a continuation of PR #59002 --- library/proc_macro/src/lib.rs | 7 ++-- src/test/ui/proc-macro/auxiliary/raw-ident.rs | 35 +++++++++++++++++++ src/test/ui/proc-macro/raw-ident.rs | 16 +++++++++ src/test/ui/proc-macro/raw-ident.stderr | 10 ++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/proc-macro/auxiliary/raw-ident.rs create mode 100644 src/test/ui/proc-macro/raw-ident.rs create mode 100644 src/test/ui/proc-macro/raw-ident.stderr diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 42ba7f5c02591..de3866d92fc3f 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -848,7 +848,7 @@ impl Ident { /// Creates a new `Ident` with the given `string` as well as the specified /// `span`. /// The `string` argument must be a valid identifier permitted by the - /// language, otherwise the function will panic. + /// language (including keywords, e.g. `self` or `fn`). Otherwise, the function will panic. /// /// Note that `span`, currently in rustc, configures the hygiene information /// for this identifier. @@ -870,7 +870,10 @@ impl Ident { } /// Same as `Ident::new`, but creates a raw identifier (`r#ident`). - #[unstable(feature = "proc_macro_raw_ident", issue = "54723")] + /// The `string` argument be a valid identifier permitted by the language + /// (including keywords, e.g. `fn`). Keywords which are usable in path segments + /// (e.g. `self`, `super`) are not supported, and will cause a panic. + #[stable(feature = "proc_macro_raw_ident", since = "1.47.0")] pub fn new_raw(string: &str, span: Span) -> Ident { Ident(bridge::client::Ident::new(string, span.0, true)) } diff --git a/src/test/ui/proc-macro/auxiliary/raw-ident.rs b/src/test/ui/proc-macro/auxiliary/raw-ident.rs new file mode 100644 index 0000000000000..9daee21aa17d4 --- /dev/null +++ b/src/test/ui/proc-macro/auxiliary/raw-ident.rs @@ -0,0 +1,35 @@ +// force-host +// no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; +use proc_macro::{TokenStream, TokenTree, Ident, Punct, Spacing, Span}; + +#[proc_macro] +pub fn make_struct(input: TokenStream) -> TokenStream { + match input.into_iter().next().unwrap() { + TokenTree::Ident(ident) => { + vec![ + TokenTree::Ident(Ident::new("struct", Span::call_site())), + TokenTree::Ident(Ident::new_raw(&ident.to_string(), Span::call_site())), + TokenTree::Punct(Punct::new(';', Spacing::Alone)) + ].into_iter().collect() + } + _ => panic!() + } +} + +#[proc_macro] +pub fn make_bad_struct(input: TokenStream) -> TokenStream { + match input.into_iter().next().unwrap() { + TokenTree::Ident(ident) => { + vec![ + TokenTree::Ident(Ident::new_raw("struct", Span::call_site())), + TokenTree::Ident(Ident::new(&ident.to_string(), Span::call_site())), + TokenTree::Punct(Punct::new(';', Spacing::Alone)) + ].into_iter().collect() + } + _ => panic!() + } +} diff --git a/src/test/ui/proc-macro/raw-ident.rs b/src/test/ui/proc-macro/raw-ident.rs new file mode 100644 index 0000000000000..03cb4571496e9 --- /dev/null +++ b/src/test/ui/proc-macro/raw-ident.rs @@ -0,0 +1,16 @@ +// aux-build:raw-ident.rs + +#[macro_use] extern crate raw_ident; + +fn main() { + make_struct!(fn); + make_struct!(Foo); + make_struct!(await); + + r#fn; + r#Foo; + Foo; + r#await; + + make_bad_struct!(S); //~ ERROR expected one of +} diff --git a/src/test/ui/proc-macro/raw-ident.stderr b/src/test/ui/proc-macro/raw-ident.stderr new file mode 100644 index 0000000000000..e82a1226b5aef --- /dev/null +++ b/src/test/ui/proc-macro/raw-ident.stderr @@ -0,0 +1,10 @@ +error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `S` + --> $DIR/raw-ident.rs:15:5 + | +LL | make_bad_struct!(S); + | ^^^^^^^^^^^^^^^^^^^^ expected one of 8 possible tokens + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + From 0a88346be64d0c64771d72ada8583e5795416556 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 2 Aug 2020 13:17:20 +0300 Subject: [PATCH 8/9] rustc_ast: `(Nested)MetaItem::check_name` -> `has_name` For consistency with `Attribute::has_name` which doesn't mark the attribute as used either. Replace all uses of `check_name` with `has_name` outside of rustc --- src/librustc_ast/attr/mod.rs | 13 +++++----- src/librustc_ast_passes/feature_gate.rs | 4 ++-- src/librustc_attr/builtin.rs | 10 ++++---- .../proc_macro_harness.rs | 2 +- src/librustc_builtin_macros/test.rs | 2 +- src/librustc_expand/expand.rs | 4 ++-- .../assert_module_sources.rs | 2 +- .../persist/dirty_clean.rs | 10 ++++---- src/librustc_lint/builtin.rs | 2 +- src/librustc_metadata/native_libs.rs | 8 +++---- src/librustc_mir/dataflow/framework/engine.rs | 4 ++-- src/librustc_mir/dataflow/mod.rs | 2 +- src/librustc_passes/check_attr.rs | 2 +- src/librustc_save_analysis/lib.rs | 4 ++-- .../traits/on_unimplemented.rs | 10 ++++---- src/librustc_typeck/collect.rs | 8 +++---- src/librustdoc/clean/mod.rs | 8 +++---- src/librustdoc/clean/types.rs | 24 +++++++++---------- src/librustdoc/html/render/cache.rs | 2 +- src/librustdoc/test.rs | 8 +++---- src/librustdoc/visit_ast.rs | 12 +++++----- src/tools/clippy/clippy_lints/src/attrs.rs | 18 +++++++------- src/tools/clippy/clippy_lints/src/doc.rs | 2 +- .../src/inline_fn_without_body.rs | 2 +- .../clippy_lints/src/manual_non_exhaustive.rs | 2 +- .../clippy/clippy_lints/src/missing_doc.rs | 2 +- .../clippy/clippy_lints/src/missing_inline.rs | 2 +- .../clippy_lints/src/needless_borrow.rs | 2 +- .../src/needless_pass_by_value.rs | 2 +- src/tools/clippy/clippy_lints/src/returns.rs | 2 +- .../src/trivially_copy_pass_by_ref.rs | 2 +- .../clippy/clippy_lints/src/utils/conf.rs | 2 +- 32 files changed, 90 insertions(+), 89 deletions(-) diff --git a/src/librustc_ast/attr/mod.rs b/src/librustc_ast/attr/mod.rs index 9d4b6dbed9870..022e49a7ea4c2 100644 --- a/src/librustc_ast/attr/mod.rs +++ b/src/librustc_ast/attr/mod.rs @@ -100,8 +100,8 @@ impl NestedMetaItem { } /// Returns `true` if this list item is a MetaItem with a name of `name`. - pub fn check_name(&self, name: Symbol) -> bool { - self.meta_item().map_or(false, |meta_item| meta_item.check_name(name)) + pub fn has_name(&self, name: Symbol) -> bool { + self.meta_item().map_or(false, |meta_item| meta_item.has_name(name)) } /// For a single-segment meta item, returns its name; otherwise, returns `None`. @@ -173,8 +173,9 @@ impl Attribute { } } - /// Returns `true` if the attribute's path matches the argument. If it matches, then the - /// attribute is marked as used. + /// Returns `true` if the attribute's path matches the argument. + /// If it matches, then the attribute is marked as used. + /// Should only be used by rustc, other tools can use `has_name` instead. pub fn check_name(&self, name: Symbol) -> bool { let matches = self.has_name(name); if matches { @@ -278,7 +279,7 @@ impl MetaItem { } } - pub fn check_name(&self, name: Symbol) -> bool { + pub fn has_name(&self, name: Symbol) -> bool { self.path == name } @@ -405,7 +406,7 @@ pub fn mk_doc_comment(style: AttrStyle, comment: Symbol, span: Span) -> Attribut } pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool { - items.iter().any(|item| item.check_name(name)) + items.iter().any(|item| item.has_name(name)) } pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool { diff --git a/src/librustc_ast_passes/feature_gate.rs b/src/librustc_ast_passes/feature_gate.rs index 2fe208c3ce68c..22eaca4f071e2 100644 --- a/src/librustc_ast_passes/feature_gate.rs +++ b/src/librustc_ast_passes/feature_gate.rs @@ -243,7 +243,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { if attr.check_name(sym::doc) { for nested_meta in attr.meta_item_list().unwrap_or_default() { macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => { - $(if nested_meta.check_name(sym::$name) { + $(if nested_meta.has_name(sym::$name) { let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental"); gate_feature_post!(self, $feature, attr.span, msg); })* @@ -314,7 +314,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { ast::ItemKind::Struct(..) => { for attr in attr::filter_by_name(&i.attrs[..], sym::repr) { for item in attr.meta_item_list().unwrap_or_else(Vec::new) { - if item.check_name(sym::simd) { + if item.has_name(sym::simd) { gate_feature_post!( &self, repr_simd, diff --git a/src/librustc_attr/builtin.rs b/src/librustc_attr/builtin.rs index 1e088b52dcc35..983202aafabd5 100644 --- a/src/librustc_attr/builtin.rs +++ b/src/librustc_attr/builtin.rs @@ -92,9 +92,9 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op if let Some(meta) = attr.meta() { if let MetaItemKind::List(items) = meta.kind { if items.len() == 1 { - if items[0].check_name(sym::allowed) { + if items[0].has_name(sym::allowed) { return Some(UnwindAttr::Allowed); - } else if items[0].check_name(sym::aborts) { + } else if items[0].has_name(sym::aborts) { return Some(UnwindAttr::Aborts); } } @@ -168,7 +168,7 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: Symbol) -> bool item.check_name(sym::feature) && item .meta_item_list() - .map(|list| list.iter().any(|mi| mi.is_word() && mi.check_name(feature_name))) + .map(|list| list.iter().any(|mi| mi.is_word() && mi.has_name(feature_name))) .unwrap_or(false) }) } @@ -505,7 +505,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat } fn try_gate_cfg(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) { - let gate = find_gated_cfg(|sym| cfg.check_name(sym)); + let gate = find_gated_cfg(|sym| cfg.has_name(sym)); if let (Some(feats), Some(gated_cfg)) = (features, gate) { gate_cfg(&gated_cfg, cfg.span, sess, feats); } @@ -898,7 +898,7 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec { } } else { if let Some(meta_item) = item.meta_item() { - if meta_item.check_name(sym::align) { + if meta_item.has_name(sym::align) { if let MetaItemKind::NameValue(ref value) = meta_item.kind { recognised = true; let mut err = struct_span_err!( diff --git a/src/librustc_builtin_macros/proc_macro_harness.rs b/src/librustc_builtin_macros/proc_macro_harness.rs index f044ce41e879e..763bdca35ebe6 100644 --- a/src/librustc_builtin_macros/proc_macro_harness.rs +++ b/src/librustc_builtin_macros/proc_macro_harness.rs @@ -143,7 +143,7 @@ impl<'a> CollectProcMacros<'a> { let attributes_attr = list.get(1); let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr { - if !attr.check_name(sym::attributes) { + if !attr.has_name(sym::attributes) { self.handler.span_err(attr.span(), "second argument must be `attributes`") } attr.meta_item_list() diff --git a/src/librustc_builtin_macros/test.rs b/src/librustc_builtin_macros/test.rs index 460f947a792af..29095034ba9f1 100644 --- a/src/librustc_builtin_macros/test.rs +++ b/src/librustc_builtin_macros/test.rs @@ -336,7 +336,7 @@ fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic { Some(list) => { let msg = list .iter() - .find(|mi| mi.check_name(sym::expected)) + .find(|mi| mi.has_name(sym::expected)) .and_then(|mi| mi.meta_item()) .and_then(|mi| mi.value_str()); if list.len() != 1 || msg.is_none() { diff --git a/src/librustc_expand/expand.rs b/src/librustc_expand/expand.rs index e4c0fcaa298d2..0cc340c205ac8 100644 --- a/src/librustc_expand/expand.rs +++ b/src/librustc_expand/expand.rs @@ -1644,14 +1644,14 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> { } if let Some(list) = at.meta_item_list() { - if !list.iter().any(|it| it.check_name(sym::include)) { + if !list.iter().any(|it| it.has_name(sym::include)) { return noop_visit_attribute(at, self); } let mut items = vec![]; for mut it in list { - if !it.check_name(sym::include) { + if !it.has_name(sym::include) { items.push({ noop_visit_meta_list_item(&mut it, self); it diff --git a/src/librustc_incremental/assert_module_sources.rs b/src/librustc_incremental/assert_module_sources.rs index cd5da7a67685c..d451d9a22a48b 100644 --- a/src/librustc_incremental/assert_module_sources.rs +++ b/src/librustc_incremental/assert_module_sources.rs @@ -149,7 +149,7 @@ impl AssertModuleSource<'tcx> { fn field(&self, attr: &ast::Attribute, name: Symbol) -> Symbol { for item in attr.meta_item_list().unwrap_or_else(Vec::new) { - if item.check_name(name) { + if item.has_name(name) { if let Some(value) = item.value_str() { return value; } else { diff --git a/src/librustc_incremental/persist/dirty_clean.rs b/src/librustc_incremental/persist/dirty_clean.rs index d48810f1cf103..02f37f82352a9 100644 --- a/src/librustc_incremental/persist/dirty_clean.rs +++ b/src/librustc_incremental/persist/dirty_clean.rs @@ -231,7 +231,7 @@ impl DirtyCleanVisitor<'tcx> { fn labels(&self, attr: &Attribute) -> Option { for item in attr.meta_item_list().unwrap_or_else(Vec::new) { - if item.check_name(LABEL) { + if item.has_name(LABEL) { let value = expect_associated_value(self.tcx, &item); return Some(self.resolve_labels(&item, value)); } @@ -242,7 +242,7 @@ impl DirtyCleanVisitor<'tcx> { /// `except=` attribute value fn except(&self, attr: &Attribute) -> Labels { for item in attr.meta_item_list().unwrap_or_else(Vec::new) { - if item.check_name(EXCEPT) { + if item.has_name(EXCEPT) { let value = expect_associated_value(self.tcx, &item); return self.resolve_labels(&item, value); } @@ -474,15 +474,15 @@ fn check_config(tcx: TyCtxt<'_>, attr: &Attribute) -> bool { debug!("check_config: config={:?}", config); let (mut cfg, mut except, mut label) = (None, false, false); for item in attr.meta_item_list().unwrap_or_else(Vec::new) { - if item.check_name(CFG) { + if item.has_name(CFG) { let value = expect_associated_value(tcx, &item); debug!("check_config: searching for cfg {:?}", value); cfg = Some(config.contains(&(value, None))); } - if item.check_name(LABEL) { + if item.has_name(LABEL) { label = true; } - if item.check_name(EXCEPT) { + if item.has_name(EXCEPT) { except = true; } } diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index e32c8fbee6852..6515708e115a5 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -330,7 +330,7 @@ fn has_doc(attr: &ast::Attribute) -> bool { if let Some(list) = attr.meta_item_list() { for meta in list { - if meta.check_name(sym::include) || meta.check_name(sym::hidden) { + if meta.has_name(sym::include) || meta.has_name(sym::hidden) { return true; } } diff --git a/src/librustc_metadata/native_libs.rs b/src/librustc_metadata/native_libs.rs index fc4235a3eda09..d01c598d059c0 100644 --- a/src/librustc_metadata/native_libs.rs +++ b/src/librustc_metadata/native_libs.rs @@ -58,7 +58,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> { let mut kind_specified = false; for item in items.iter() { - if item.check_name(sym::kind) { + if item.has_name(sym::kind) { kind_specified = true; let kind = match item.value_str() { Some(name) => name, @@ -84,9 +84,9 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> { NativeLibKind::Unspecified } }; - } else if item.check_name(sym::name) { + } else if item.has_name(sym::name) { lib.name = item.value_str(); - } else if item.check_name(sym::cfg) { + } else if item.has_name(sym::cfg) { let cfg = match item.meta_item_list() { Some(list) => list, None => continue, // skip like historical compilers @@ -98,7 +98,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> { } else { self.tcx.sess.span_err(cfg[0].span(), "invalid argument for `cfg(..)`"); } - } else if item.check_name(sym::wasm_import_module) { + } else if item.has_name(sym::wasm_import_module) { match item.value_str() { Some(s) => lib.wasm_import_module = Some(s), None => { diff --git a/src/librustc_mir/dataflow/framework/engine.rs b/src/librustc_mir/dataflow/framework/engine.rs index 003c40f290b8d..2113d40a594eb 100644 --- a/src/librustc_mir/dataflow/framework/engine.rs +++ b/src/librustc_mir/dataflow/framework/engine.rs @@ -339,7 +339,7 @@ impl RustcMirAttrs { .flat_map(|attr| attr.meta_item_list().into_iter().flat_map(|v| v.into_iter())); for attr in rustc_mir_attrs { - let attr_result = if attr.check_name(sym::borrowck_graphviz_postflow) { + let attr_result = if attr.has_name(sym::borrowck_graphviz_postflow) { Self::set_field(&mut ret.basename_and_suffix, tcx, &attr, |s| { let path = PathBuf::from(s.to_string()); match path.file_name() { @@ -350,7 +350,7 @@ impl RustcMirAttrs { } } }) - } else if attr.check_name(sym::borrowck_graphviz_format) { + } else if attr.has_name(sym::borrowck_graphviz_format) { Self::set_field(&mut ret.formatter, tcx, &attr, |s| match s { sym::gen_kill | sym::two_phase => Ok(s), _ => { diff --git a/src/librustc_mir/dataflow/mod.rs b/src/librustc_mir/dataflow/mod.rs index ae1328dbd12c7..8a9edb23a10ef 100644 --- a/src/librustc_mir/dataflow/mod.rs +++ b/src/librustc_mir/dataflow/mod.rs @@ -34,7 +34,7 @@ pub(crate) fn has_rustc_mir_with(attrs: &[ast::Attribute], name: Symbol) -> Opti let items = attr.meta_item_list(); for item in items.iter().flat_map(|l| l.iter()) { match item.meta_item() { - Some(mi) if mi.check_name(name) => return Some(mi.clone()), + Some(mi) if mi.has_name(name) => return Some(mi.clone()), _ => continue, } } diff --git a/src/librustc_passes/check_attr.rs b/src/librustc_passes/check_attr.rs index 3e63a63d9d0f9..1ff47ee038d3b 100644 --- a/src/librustc_passes/check_attr.rs +++ b/src/librustc_passes/check_attr.rs @@ -222,7 +222,7 @@ impl CheckAttrVisitor<'tcx> { if let Some(mi) = attr.meta() { if let Some(list) = mi.meta_item_list() { for meta in list { - if meta.check_name(sym::alias) { + if meta.has_name(sym::alias) { if !meta.is_value_str() || meta .value_str() diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index 0751dbb027ae2..8e379a3510038 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -832,10 +832,10 @@ impl<'tcx> SaveContext<'tcx> { if let Some(meta_list) = attr.meta_item_list() { meta_list .into_iter() - .filter(|it| it.check_name(sym::include)) + .filter(|it| it.has_name(sym::include)) .filter_map(|it| it.meta_item_list().map(|l| l.to_owned())) .flat_map(|it| it) - .filter(|meta| meta.check_name(sym::contents)) + .filter(|meta| meta.has_name(sym::contents)) .filter_map(|meta| meta.value_str()) .for_each(|val| { result.push_str(&val.as_str()); diff --git a/src/librustc_trait_selection/traits/on_unimplemented.rs b/src/librustc_trait_selection/traits/on_unimplemented.rs index deb33708681fa..446d5a489df48 100644 --- a/src/librustc_trait_selection/traits/on_unimplemented.rs +++ b/src/librustc_trait_selection/traits/on_unimplemented.rs @@ -95,27 +95,27 @@ impl<'tcx> OnUnimplementedDirective { }; for item in item_iter { - if item.check_name(sym::message) && message.is_none() { + if item.has_name(sym::message) && message.is_none() { if let Some(message_) = item.value_str() { message = parse_value(message_)?; continue; } - } else if item.check_name(sym::label) && label.is_none() { + } else if item.has_name(sym::label) && label.is_none() { if let Some(label_) = item.value_str() { label = parse_value(label_)?; continue; } - } else if item.check_name(sym::note) && note.is_none() { + } else if item.has_name(sym::note) && note.is_none() { if let Some(note_) = item.value_str() { note = parse_value(note_)?; continue; } - } else if item.check_name(sym::enclosing_scope) && enclosing_scope.is_none() { + } else if item.has_name(sym::enclosing_scope) && enclosing_scope.is_none() { if let Some(enclosing_scope_) = item.value_str() { enclosing_scope = parse_value(enclosing_scope_)?; continue; } - } else if item.check_name(sym::on) + } else if item.has_name(sym::on) && is_root && message.is_none() && label.is_none() diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 8715dacb324b0..97df065500a16 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -2231,7 +2231,7 @@ fn from_target_feature( let rust_features = tcx.features(); for item in list { // Only `enable = ...` is accepted in the meta-item list. - if !item.check_name(sym::enable) { + if !item.has_name(sym::enable) { bad_item(item.span()); continue; } @@ -2483,11 +2483,11 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { no_sanitize_span = Some(attr.span); if let Some(list) = attr.meta_item_list() { for item in list.iter() { - if item.check_name(sym::address) { + if item.has_name(sym::address) { codegen_fn_attrs.no_sanitize |= SanitizerSet::ADDRESS; - } else if item.check_name(sym::memory) { + } else if item.has_name(sym::memory) { codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMORY; - } else if item.check_name(sym::thread) { + } else if item.has_name(sym::thread) { codegen_fn_attrs.no_sanitize |= SanitizerSet::THREAD; } else { tcx.sess diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 5b048372624a4..2a090d6efa5fd 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -113,7 +113,7 @@ impl Clean for CrateNum { let mut prim = None; for attr in attrs.lists(sym::doc) { if let Some(v) = attr.value_str() { - if attr.check_name(sym::primitive) { + if attr.has_name(sym::primitive) { prim = PrimitiveType::from_symbol(v); if prim.is_some() { break; @@ -168,7 +168,7 @@ impl Clean for CrateNum { let mut keyword = None; for attr in attrs.lists(sym::doc) { if let Some(v) = attr.value_str() { - if attr.check_name(sym::keyword) { + if attr.has_name(sym::keyword) { if v.is_doc_keyword() { keyword = Some(v.to_string()); break; @@ -2157,7 +2157,7 @@ impl Clean> for doctree::ExternCrate<'_> { fn clean(&self, cx: &DocContext<'_>) -> Vec { let please_inline = self.vis.node.is_pub() && self.attrs.iter().any(|a| { - a.check_name(sym::doc) + a.has_name(sym::doc) && match a.meta_item_list() { Some(l) => attr::list_contains_name(&l, sym::inline), None => false, @@ -2197,7 +2197,7 @@ impl Clean> for doctree::Import<'_> { // Don't inline doc(hidden) imports so they can be stripped at a later stage. let mut denied = !self.vis.node.is_pub() || self.attrs.iter().any(|a| { - a.check_name(sym::doc) + a.has_name(sym::doc) && match a.meta_item_list() { Some(l) => { attr::list_contains_name(&l, sym::no_inline) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 89549eae2cb0e..1bea41b658532 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -210,7 +210,7 @@ impl Item { } pub fn is_non_exhaustive(&self) -> bool { - self.attrs.other_attrs.iter().any(|a| a.check_name(sym::non_exhaustive)) + self.attrs.other_attrs.iter().any(|a| a.has_name(sym::non_exhaustive)) } /// Returns a documentation-level item type from the item. @@ -309,7 +309,7 @@ impl<'a> Iterator for ListAttributesIter<'a> { for attr in &mut self.attrs { if let Some(list) = attr.meta_item_list() { - if attr.check_name(self.name) { + if attr.has_name(self.name) { self.current_list = list.into_iter(); if let Some(nested) = self.current_list.next() { return Some(nested); @@ -345,7 +345,7 @@ pub trait NestedAttributesExt { impl> NestedAttributesExt for I { fn has_word(self, word: Symbol) -> bool { - self.into_iter().any(|attr| attr.is_word() && attr.check_name(word)) + self.into_iter().any(|attr| attr.is_word() && attr.has_name(word)) } } @@ -425,7 +425,7 @@ impl Attributes { if let ast::MetaItemKind::List(ref nmis) = mi.kind { if nmis.len() == 1 { if let MetaItem(ref cfg_mi) = nmis[0] { - if cfg_mi.check_name(sym::cfg) { + if cfg_mi.has_name(sym::cfg) { if let ast::MetaItemKind::List(ref cfg_nmis) = cfg_mi.kind { if cfg_nmis.len() == 1 { if let MetaItem(ref content_mi) = cfg_nmis[0] { @@ -447,7 +447,7 @@ impl Attributes { pub fn extract_include(mi: &ast::MetaItem) -> Option<(String, String)> { mi.meta_item_list().and_then(|list| { for meta in list { - if meta.check_name(sym::include) { + if meta.has_name(sym::include) { // the actual compiled `#[doc(include="filename")]` gets expanded to // `#[doc(include(file="filename", contents="file contents")]` so we need to // look for that instead @@ -456,11 +456,11 @@ impl Attributes { let mut contents: Option = None; for it in list { - if it.check_name(sym::file) { + if it.has_name(sym::file) { if let Some(name) = it.value_str() { filename = Some(name.to_string()); } - } else if it.check_name(sym::contents) { + } else if it.has_name(sym::contents) { if let Some(docs) = it.value_str() { contents = Some(docs.to_string()); } @@ -482,12 +482,12 @@ impl Attributes { pub fn has_doc_flag(&self, flag: Symbol) -> bool { for attr in &self.other_attrs { - if !attr.check_name(sym::doc) { + if !attr.has_name(sym::doc) { continue; } if let Some(items) = attr.meta_item_list() { - if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name(flag)) { + if items.iter().filter_map(|i| i.meta_item()).any(|it| it.has_name(flag)) { return true; } } @@ -521,7 +521,7 @@ impl Attributes { } None } else { - if attr.check_name(sym::doc) { + if attr.has_name(sym::doc) { if let Some(mi) = attr.meta() { if let Some(cfg_mi) = Attributes::extract_cfg(&mi) { // Extracted #[doc(cfg(...))] @@ -548,7 +548,7 @@ impl Attributes { // treat #[target_feature(enable = "feat")] attributes as if they were // #[doc(cfg(target_feature = "feat"))] attributes as well for attr in attrs.lists(sym::target_feature) { - if attr.check_name(sym::enable) { + if attr.has_name(sym::enable) { if let Some(feat) = attr.value_str() { let meta = attr::mk_name_value_item_str( Ident::with_dummy_span(sym::target_feature), @@ -648,7 +648,7 @@ impl Attributes { pub fn get_doc_aliases(&self) -> FxHashSet { self.other_attrs .lists(sym::doc) - .filter(|a| a.check_name(sym::alias)) + .filter(|a| a.has_name(sym::alias)) .filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", ""))) .filter(|v| !v.is_empty()) .collect::>() diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 378efa1a1bed7..5a9e9dda6771e 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -48,7 +48,7 @@ pub fn extern_location( // external crate e.attrs .lists(sym::doc) - .filter(|a| a.check_name(sym::html_root_url)) + .filter(|a| a.has_name(sym::html_root_url)) .filter_map(|a| a.value_str()) .map(|url| { let mut url = url.to_string(); diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index e8ea71997109a..ba1341e652c39 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -175,17 +175,17 @@ fn scrape_test_config(krate: &::rustc_hir::Crate<'_>) -> TestOptions { .item .attrs .iter() - .filter(|a| a.check_name(sym::doc)) + .filter(|a| a.has_name(sym::doc)) .flat_map(|a| a.meta_item_list().unwrap_or_else(Vec::new)) - .filter(|a| a.check_name(sym::test)) + .filter(|a| a.has_name(sym::test)) .collect(); let attrs = test_attrs.iter().flat_map(|a| a.meta_item_list().unwrap_or(&[])); for attr in attrs { - if attr.check_name(sym::no_crate_inject) { + if attr.has_name(sym::no_crate_inject) { opts.no_crate_inject = true; } - if attr.check_name(sym::attr) { + if attr.has_name(sym::attr) { if let Some(l) = attr.meta_item_list() { for item in l { opts.attrs.push(pprust::meta_list_item_to_string(item)); diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 735446d235c2e..cf57ffd0b4b30 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -165,11 +165,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { ) { debug!("visiting fn"); let macro_kind = item.attrs.iter().find_map(|a| { - if a.check_name(sym::proc_macro) { + if a.has_name(sym::proc_macro) { Some(MacroKind::Bang) - } else if a.check_name(sym::proc_macro_derive) { + } else if a.has_name(sym::proc_macro_derive) { Some(MacroKind::Derive) - } else if a.check_name(sym::proc_macro_attribute) { + } else if a.has_name(sym::proc_macro_attribute) { Some(MacroKind::Attr) } else { None @@ -189,7 +189,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let mut helpers = Vec::new(); for mi in item.attrs.lists(sym::proc_macro_derive) { - if !mi.check_name(sym::attributes) { + if !mi.has_name(sym::attributes) { continue; } @@ -419,8 +419,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // anything as it will probably be stripped anyway. if item.vis.node.is_pub() && self.inside_public_path { let please_inline = item.attrs.iter().any(|item| match item.meta_item_list() { - Some(ref list) if item.check_name(sym::doc) => { - list.iter().any(|i| i.check_name(sym::inline)) + Some(ref list) if item.has_name(sym::doc) => { + list.iter().any(|i| i.has_name(sym::inline)) } _ => false, }); diff --git a/src/tools/clippy/clippy_lints/src/attrs.rs b/src/tools/clippy/clippy_lints/src/attrs.rs index 27a7fa8862237..40af6bb3d7bcf 100644 --- a/src/tools/clippy/clippy_lints/src/attrs.rs +++ b/src/tools/clippy/clippy_lints/src/attrs.rs @@ -286,14 +286,14 @@ impl<'tcx> LateLintPass<'tcx> for Attributes { }, _ => {}, } - if items.is_empty() || !attr.check_name(sym!(deprecated)) { + if items.is_empty() || !attr.has_name(sym!(deprecated)) { return; } for item in items { if_chain! { if let NestedMetaItem::MetaItem(mi) = &item; if let MetaItemKind::NameValue(lit) = &mi.kind; - if mi.check_name(sym!(since)); + if mi.has_name(sym!(since)); then { check_semver(cx, item.span(), lit); } @@ -309,7 +309,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes { } match item.kind { ItemKind::ExternCrate(..) | ItemKind::Use(..) => { - let skip_unused_imports = item.attrs.iter().any(|attr| attr.check_name(sym!(macro_use))); + let skip_unused_imports = item.attrs.iter().any(|attr| attr.has_name(sym!(macro_use))); for attr in item.attrs { if in_external_macro(cx.sess(), attr.span) { @@ -524,7 +524,7 @@ fn check_attrs(cx: &LateContext<'_>, span: Span, name: Name, attrs: &[Attribute] for attr in attrs { if let Some(values) = attr.meta_item_list() { - if values.len() != 1 || !attr.check_name(sym!(inline)) { + if values.len() != 1 || !attr.has_name(sym!(inline)) { continue; } if is_word(&values[0], sym!(always)) { @@ -558,7 +558,7 @@ fn check_semver(cx: &LateContext<'_>, span: Span, lit: &Lit) { fn is_word(nmi: &NestedMetaItem, expected: Symbol) -> bool { if let NestedMetaItem::MetaItem(mi) = &nmi { - mi.is_word() && mi.check_name(expected) + mi.is_word() && mi.has_name(expected) } else { false } @@ -618,15 +618,15 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::as fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute) { if_chain! { // check cfg_attr - if attr.check_name(sym!(cfg_attr)); + if attr.has_name(sym!(cfg_attr)); if let Some(items) = attr.meta_item_list(); if items.len() == 2; // check for `rustfmt` if let Some(feature_item) = items[0].meta_item(); - if feature_item.check_name(sym!(rustfmt)); + if feature_item.has_name(sym!(rustfmt)); // check for `rustfmt_skip` and `rustfmt::skip` if let Some(skip_item) = &items[1].meta_item(); - if skip_item.check_name(sym!(rustfmt_skip)) || + if skip_item.has_name(sym!(rustfmt_skip)) || skip_item.path.segments.last().expect("empty path in attribute").ident.name == sym!(skip); // Only lint outer attributes, because custom inner attributes are unstable // Tracking issue: https://github.com/rust-lang/rust/issues/54726 @@ -685,7 +685,7 @@ fn check_mismatched_target_os(cx: &EarlyContext<'_>, attr: &Attribute) { } if_chain! { - if attr.check_name(sym!(cfg)); + if attr.has_name(sym!(cfg)); if let Some(list) = attr.meta_item_list(); let mismatched = find_mismatched_target_os(&list); if !mismatched.is_empty(); diff --git a/src/tools/clippy/clippy_lints/src/doc.rs b/src/tools/clippy/clippy_lints/src/doc.rs index d52bb8961fae7..e87c33d1b09dd 100644 --- a/src/tools/clippy/clippy_lints/src/doc.rs +++ b/src/tools/clippy/clippy_lints/src/doc.rs @@ -323,7 +323,7 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet, attrs let (comment, current_spans) = strip_doc_comment_decoration(&comment, attr.span); spans.extend_from_slice(¤t_spans); doc.push_str(&comment); - } else if attr.check_name(sym!(doc)) { + } else if attr.has_name(sym!(doc)) { // ignore mix of sugared and non-sugared doc // don't trigger the safety or errors check return DocHeaders { diff --git a/src/tools/clippy/clippy_lints/src/inline_fn_without_body.rs b/src/tools/clippy/clippy_lints/src/inline_fn_without_body.rs index decbee278154a..4b605fdb366a9 100644 --- a/src/tools/clippy/clippy_lints/src/inline_fn_without_body.rs +++ b/src/tools/clippy/clippy_lints/src/inline_fn_without_body.rs @@ -41,7 +41,7 @@ impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody { fn check_attrs(cx: &LateContext<'_>, name: Symbol, attrs: &[Attribute]) { for attr in attrs { - if !attr.check_name(sym!(inline)) { + if !attr.has_name(sym!(inline)) { continue; } diff --git a/src/tools/clippy/clippy_lints/src/manual_non_exhaustive.rs b/src/tools/clippy/clippy_lints/src/manual_non_exhaustive.rs index f3b8902e26f67..ca1381852daee 100644 --- a/src/tools/clippy/clippy_lints/src/manual_non_exhaustive.rs +++ b/src/tools/clippy/clippy_lints/src/manual_non_exhaustive.rs @@ -83,7 +83,7 @@ fn check_manual_non_exhaustive_enum(cx: &EarlyContext<'_>, item: &Item, variants } fn is_doc_hidden(attr: &Attribute) -> bool { - attr.check_name(sym!(doc)) + attr.has_name(sym!(doc)) && match attr.meta_item_list() { Some(l) => attr::list_contains_name(&l, sym!(hidden)), None => false, diff --git a/src/tools/clippy/clippy_lints/src/missing_doc.rs b/src/tools/clippy/clippy_lints/src/missing_doc.rs index 06e0f43c10bb8..813f9c4394819 100644 --- a/src/tools/clippy/clippy_lints/src/missing_doc.rs +++ b/src/tools/clippy/clippy_lints/src/missing_doc.rs @@ -105,7 +105,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, attrs: &'tcx [ast::Attribute]) { let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| { - attr.check_name(sym!(doc)) + attr.has_name(sym!(doc)) && match attr.meta_item_list() { None => false, Some(l) => attr::list_contains_name(&l[..], sym!(hidden)), diff --git a/src/tools/clippy/clippy_lints/src/missing_inline.rs b/src/tools/clippy/clippy_lints/src/missing_inline.rs index 9c96267353701..3eae45b2819d8 100644 --- a/src/tools/clippy/clippy_lints/src/missing_inline.rs +++ b/src/tools/clippy/clippy_lints/src/missing_inline.rs @@ -57,7 +57,7 @@ declare_clippy_lint! { } fn check_missing_inline_attrs(cx: &LateContext<'_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) { - let has_inline = attrs.iter().any(|a| a.check_name(sym!(inline))); + let has_inline = attrs.iter().any(|a| a.has_name(sym!(inline))); if !has_inline { span_lint( cx, diff --git a/src/tools/clippy/clippy_lints/src/needless_borrow.rs b/src/tools/clippy/clippy_lints/src/needless_borrow.rs index 415ab556c9fd4..9391049c6e8f9 100644 --- a/src/tools/clippy/clippy_lints/src/needless_borrow.rs +++ b/src/tools/clippy/clippy_lints/src/needless_borrow.rs @@ -112,7 +112,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow { } fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) { - if item.attrs.iter().any(|a| a.check_name(sym!(automatically_derived))) { + if item.attrs.iter().any(|a| a.has_name(sym!(automatically_derived))) { debug_assert!(self.derived_item.is_none()); self.derived_item = Some(item.hir_id); } diff --git a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs index 0957787774498..a7f7c97fc487c 100644 --- a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs +++ b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs @@ -312,7 +312,7 @@ fn requires_exact_signature(attrs: &[Attribute]) -> bool { attrs.iter().any(|attr| { [sym!(proc_macro), sym!(proc_macro_attribute), sym!(proc_macro_derive)] .iter() - .any(|&allow| attr.check_name(allow)) + .any(|&allow| attr.has_name(allow)) }) } diff --git a/src/tools/clippy/clippy_lints/src/returns.rs b/src/tools/clippy/clippy_lints/src/returns.rs index faef7e724dd05..8ed20995a70af 100644 --- a/src/tools/clippy/clippy_lints/src/returns.rs +++ b/src/tools/clippy/clippy_lints/src/returns.rs @@ -235,7 +235,7 @@ impl EarlyLintPass for Return { } fn attr_is_cfg(attr: &ast::Attribute) -> bool { - attr.meta_item_list().is_some() && attr.check_name(sym!(cfg)) + attr.meta_item_list().is_some() && attr.has_name(sym!(cfg)) } // get the def site diff --git a/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs b/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs index 6a2b05e3e6df7..7948d99162b81 100644 --- a/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs +++ b/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs @@ -155,7 +155,7 @@ impl<'tcx> LateLintPass<'tcx> for TriviallyCopyPassByRef { return; } for a in attrs { - if a.meta_item_list().is_some() && a.check_name(sym!(proc_macro_derive)) { + if a.meta_item_list().is_some() && a.has_name(sym!(proc_macro_derive)) { return; } } diff --git a/src/tools/clippy/clippy_lints/src/utils/conf.rs b/src/tools/clippy/clippy_lints/src/utils/conf.rs index de425211e38ef..ba3492a6fff11 100644 --- a/src/tools/clippy/clippy_lints/src/utils/conf.rs +++ b/src/tools/clippy/clippy_lints/src/utils/conf.rs @@ -13,7 +13,7 @@ use std::{env, fmt, fs, io}; /// Gets the configuration file from arguments. pub fn file_from_args(args: &[NestedMetaItem]) -> Result, (&'static str, Span)> { for arg in args.iter().filter_map(NestedMetaItem::meta_item) { - if arg.check_name(sym!(conf_file)) { + if arg.has_name(sym!(conf_file)) { return match arg.kind { MetaItemKind::Word | MetaItemKind::List(_) => Err(("`conf_file` must be a named value", arg.span)), MetaItemKind::NameValue(ref value) => { From 05f414b72a26b285c7a7f6c9632945ab567a9757 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 4 Aug 2020 00:33:56 +0300 Subject: [PATCH 9/9] rustc_ast: More detailed docs for `Attribute::check_name` --- src/librustc_ast/attr/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/librustc_ast/attr/mod.rs b/src/librustc_ast/attr/mod.rs index 022e49a7ea4c2..809fda865422a 100644 --- a/src/librustc_ast/attr/mod.rs +++ b/src/librustc_ast/attr/mod.rs @@ -175,7 +175,11 @@ impl Attribute { /// Returns `true` if the attribute's path matches the argument. /// If it matches, then the attribute is marked as used. - /// Should only be used by rustc, other tools can use `has_name` instead. + /// Should only be used by rustc, other tools can use `has_name` instead, + /// because only rustc is supposed to report the `unused_attributes` lint. + /// `MetaItem` and `NestedMetaItem` are produced by "lowering" an `Attribute` + /// and don't have identity, so they only has the `has_name` method, + /// and you need to mark the original `Attribute` as used when necessary. pub fn check_name(&self, name: Symbol) -> bool { let matches = self.has_name(name); if matches {