From f5b896451ac44fc45bdc78656176f2846f44891a Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 24 Dec 2019 03:06:52 +0700 Subject: [PATCH] do minor cleanups * ToString and AsRef are in prelude, no need to import them --- clippy_dev/src/lib.rs | 2 +- clippy_lints/src/cargo_common_metadata.rs | 4 +- clippy_lints/src/consts.rs | 2 +- clippy_lints/src/doc.rs | 1 - clippy_lints/src/eta_reduction.rs | 6 +- clippy_lints/src/lib.rs | 11 ++-- clippy_lints/src/literal_representation.rs | 1 - clippy_lints/src/multiple_crate_versions.rs | 1 - clippy_lints/src/regex.rs | 1 - clippy_lints/src/utils/conf.rs | 2 - clippy_lints/src/utils/sugg.rs | 2 - clippy_lints/src/wildcard_dependencies.rs | 2 - src/driver.rs | 67 +++++++++------------ tests/integration.rs | 1 - tests/ui/inefficient_to_string.fixed | 1 - tests/ui/inefficient_to_string.rs | 1 - tests/ui/inefficient_to_string.stderr | 12 ++-- 17 files changed, 44 insertions(+), 73 deletions(-) diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index f73e7b86720..3aae3e53317 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -50,7 +50,7 @@ impl Lint { name: name.to_lowercase(), group: group.to_string(), desc: NL_ESCAPE_RE.replace(&desc.replace("\\\"", "\""), "").to_string(), - deprecation: deprecation.map(std::string::ToString::to_string), + deprecation: deprecation.map(ToString::to_string), module: module.to_string(), } } diff --git a/clippy_lints/src/cargo_common_metadata.rs b/clippy_lints/src/cargo_common_metadata.rs index 4fc9c3e60e7..15457e57860 100644 --- a/clippy_lints/src/cargo_common_metadata.rs +++ b/clippy_lints/src/cargo_common_metadata.rs @@ -8,8 +8,6 @@ use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc_session::declare_tool_lint; use syntax::{ast::*, source_map::DUMMY_SP}; -use cargo_metadata; - declare_clippy_lint! { /// **What it does:** Checks to see if all common metadata is defined in /// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata @@ -56,7 +54,7 @@ fn is_empty_path(value: &Option) -> bool { fn is_empty_vec(value: &[String]) -> bool { // This works because empty iterators return true - value.iter().all(std::string::String::is_empty) + value.iter().all(String::is_empty) } declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]); diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index d86766ee0a5..f1e488a7e6d 100644 --- a/clippy_lints/src/consts.rs +++ b/clippy_lints/src/consts.rs @@ -253,7 +253,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { let res = self.tables.qpath_res(qpath, callee.hir_id); if let Some(def_id) = res.opt_def_id(); let def_path: Vec<_> = self.lcx.get_def_path(def_id).into_iter().map(Symbol::as_str).collect(); - let def_path: Vec<&str> = def_path.iter().map(|s| &**s).collect(); + let def_path: Vec<&str> = def_path.iter().take(4).map(|s| &**s).collect(); if let ["core", "num", int_impl, "max_value"] = *def_path; then { let value = match int_impl { diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index 327c18e855d..75e6cebb563 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -1,6 +1,5 @@ use crate::utils::{match_type, paths, return_ty, span_lint}; use itertools::Itertools; -use pulldown_cmark; use rustc::hir; use rustc::impl_lint_pass; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 54fdde67dec..0abe757e359 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -146,11 +146,7 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) { } /// Tries to determine the type for universal function call to be used instead of the closure -fn get_ufcs_type_name( - cx: &LateContext<'_, '_>, - method_def_id: def_id::DefId, - self_arg: &Expr, -) -> std::option::Option { +fn get_ufcs_type_name(cx: &LateContext<'_, '_>, method_def_id: def_id::DefId, self_arg: &Expr) -> Option { let expected_type_of_self = &cx.tcx.fn_sig(method_def_id).inputs_and_output().skip_binder()[0]; let actual_type_of_self = &cx.tables.node_type(self_arg.hir_id); diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 2e5ce50bf34..c3712e13d3f 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -47,7 +47,8 @@ extern crate syntax_pos; use rustc::lint::{self, LintId}; use rustc::session::Session; use rustc_data_structures::fx::FxHashSet; -use toml; + +use std::path::Path; /// Macro used to declare a Clippy lint. /// @@ -349,16 +350,16 @@ pub fn read_conf(args: &[syntax::ast::NestedMetaItem], sess: &Session) -> Conf { let file_name = file_name.map(|file_name| { if file_name.is_relative() { sess.local_crate_source_file - .as_ref() - .and_then(|file| std::path::Path::new(&file).parent().map(std::path::Path::to_path_buf)) - .unwrap_or_default() + .as_deref() + .and_then(Path::parent) + .unwrap_or_else(|| Path::new("")) .join(file_name) } else { file_name } }); - let (conf, errors) = utils::conf::read(file_name.as_ref().map(std::convert::AsRef::as_ref)); + let (conf, errors) = utils::conf::read(file_name.as_ref().map(AsRef::as_ref)); // all conf errors are non-fatal, we just use the default conf in case of error for error in errors { diff --git a/clippy_lints/src/literal_representation.rs b/clippy_lints/src/literal_representation.rs index 24f40a161be..427243d4c62 100644 --- a/clippy_lints/src/literal_representation.rs +++ b/clippy_lints/src/literal_representation.rs @@ -8,7 +8,6 @@ use rustc::{declare_lint_pass, impl_lint_pass}; use rustc_errors::Applicability; use rustc_session::declare_tool_lint; use syntax::ast::*; -use syntax_pos; declare_clippy_lint! { /// **What it does:** Warns if a long integral or floating-point constant does diff --git a/clippy_lints/src/multiple_crate_versions.rs b/clippy_lints/src/multiple_crate_versions.rs index db22631104d..79caf4f5552 100644 --- a/clippy_lints/src/multiple_crate_versions.rs +++ b/clippy_lints/src/multiple_crate_versions.rs @@ -6,7 +6,6 @@ use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc_session::declare_tool_lint; use syntax::{ast::*, source_map::DUMMY_SP}; -use cargo_metadata; use itertools::Itertools; declare_clippy_lint! { diff --git a/clippy_lints/src/regex.rs b/clippy_lints/src/regex.rs index e06ebe539e7..c60912ddb2c 100644 --- a/clippy_lints/src/regex.rs +++ b/clippy_lints/src/regex.rs @@ -1,7 +1,6 @@ use crate::consts::{constant, Constant}; use crate::utils::{is_expn_of, match_def_path, match_type, paths, span_help_and_lint, span_lint}; use if_chain::if_chain; -use regex_syntax; use rustc::hir::*; use rustc::impl_lint_pass; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index a3407d1e990..52223aa7a4b 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -8,7 +8,6 @@ use std::io::Read; use std::sync::Mutex; use std::{env, fmt, fs, io, path}; use syntax::{ast, source_map}; -use toml; /// Gets the configuration file from arguments. pub fn file_from_args(args: &[ast::NestedMetaItem]) -> Result, (&'static str, source_map::Span)> { @@ -77,7 +76,6 @@ macro_rules! define_Conf { } $( mod $rust_name { - use serde; use serde::Deserialize; crate fn deserialize<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result { diff --git a/clippy_lints/src/utils/sugg.rs b/clippy_lints/src/utils/sugg.rs index 228fda9eec0..7050ea4cb2d 100644 --- a/clippy_lints/src/utils/sugg.rs +++ b/clippy_lints/src/utils/sugg.rs @@ -5,9 +5,7 @@ use crate::utils::{higher, snippet, snippet_opt, snippet_with_macro_callsite}; use matches::matches; use rustc::hir; use rustc::lint::{EarlyContext, LateContext, LintContext}; -use rustc_errors; use rustc_errors::Applicability; -use std; use std::borrow::Cow; use std::convert::TryInto; use std::fmt::Display; diff --git a/clippy_lints/src/wildcard_dependencies.rs b/clippy_lints/src/wildcard_dependencies.rs index bd865bf495c..0a302ce04ca 100644 --- a/clippy_lints/src/wildcard_dependencies.rs +++ b/clippy_lints/src/wildcard_dependencies.rs @@ -4,9 +4,7 @@ use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc_session::declare_tool_lint; use syntax::{ast::*, source_map::DUMMY_SP}; -use cargo_metadata; use if_chain::if_chain; -use semver; declare_clippy_lint! { /// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`. diff --git a/src/driver.rs b/src/driver.rs index 4cdff783788..4a080c12e44 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -1,5 +1,6 @@ #![cfg_attr(feature = "deny-warnings", deny(warnings))] #![feature(rustc_private)] +#![feature(str_strip)] // FIXME: switch to something more ergonomic here, once available. // (Currently there is no way to opt into sysroot crates without `extern crate`.) @@ -18,6 +19,8 @@ use rustc_tools_util::*; use lazy_static::lazy_static; use std::borrow::Cow; +use std::env; +use std::ops::Deref; use std::panic; use std::path::{Path, PathBuf}; use std::process::{exit, Command}; @@ -26,22 +29,21 @@ mod lintlist; /// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If /// true, then return it. The parameter is assumed to be either `--arg=value` or `--arg value`. -fn arg_value<'a>( - args: impl IntoIterator, +fn arg_value<'a, T: Deref>( + args: &'a [T], find_arg: &str, pred: impl Fn(&str) -> bool, ) -> Option<&'a str> { - let mut args = args.into_iter().map(String::as_str); - + let mut args = args.iter().map(Deref::deref); while let Some(arg) = args.next() { - let arg: Vec<_> = arg.splitn(2, '=').collect(); - if arg.get(0) != Some(&find_arg) { + let mut arg = arg.splitn(2, '='); + if arg.next() != Some(find_arg) { continue; } - let value = arg.get(1).cloned().or_else(|| args.next()); - if value.as_ref().map_or(false, |p| pred(p)) { - return value; + match arg.next().or_else(|| args.next()) { + Some(v) if pred(v) => return Some(v), + _ => {}, } } None @@ -49,19 +51,16 @@ fn arg_value<'a>( #[test] fn test_arg_value() { - let args: Vec<_> = ["--bar=bar", "--foobar", "123", "--foo"] - .iter() - .map(std::string::ToString::to_string) - .collect(); + let args = &["--bar=bar", "--foobar", "123", "--foo"]; - assert_eq!(arg_value(None, "--foobar", |_| true), None); - assert_eq!(arg_value(&args, "--bar", |_| false), None); - assert_eq!(arg_value(&args, "--bar", |_| true), Some("bar")); - assert_eq!(arg_value(&args, "--bar", |p| p == "bar"), Some("bar")); - assert_eq!(arg_value(&args, "--bar", |p| p == "foo"), None); - assert_eq!(arg_value(&args, "--foobar", |p| p == "foo"), None); - assert_eq!(arg_value(&args, "--foobar", |p| p == "123"), Some("123")); - assert_eq!(arg_value(&args, "--foo", |_| true), None); + assert_eq!(arg_value(&[] as &[&str], "--foobar", |_| true), None); + assert_eq!(arg_value(args, "--bar", |_| false), None); + assert_eq!(arg_value(args, "--bar", |_| true), Some("bar")); + assert_eq!(arg_value(args, "--bar", |p| p == "bar"), Some("bar")); + assert_eq!(arg_value(args, "--bar", |p| p == "foo"), None); + assert_eq!(arg_value(args, "--foobar", |p| p == "foo"), None); + assert_eq!(arg_value(args, "--foobar", |p| p == "123"), Some("123")); + assert_eq!(arg_value(args, "--foo", |_| true), None); } #[allow(clippy::too_many_lines)] @@ -276,7 +275,7 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) { } // If backtraces are enabled, also print the query stack - let backtrace = std::env::var_os("RUST_BACKTRACE").map_or(false, |x| &x != "0"); + let backtrace = env::var_os("RUST_BACKTRACE").map_or(false, |x| &x != "0"); if backtrace { TyCtxt::try_print_query_stack(&handler); @@ -288,16 +287,14 @@ pub fn main() { lazy_static::initialize(&ICE_HOOK); exit( rustc_driver::catch_fatal_errors(move || { - use std::env; + let mut orig_args: Vec = env::args().collect(); - if std::env::args().any(|a| a == "--version" || a == "-V") { + if orig_args.iter().any(|a| a == "--version" || a == "-V") { let version_info = rustc_tools_util::get_version_info!(); println!("{}", version_info); exit(0); } - let mut orig_args: Vec = env::args().collect(); - // Get the sysroot, looking from most specific to this invocation to the least: // - command line // - runtime environment @@ -350,7 +347,7 @@ pub fn main() { } let should_describe_lints = || { - let args: Vec<_> = std::env::args().collect(); + let args: Vec<_> = env::args().collect(); args.windows(2).any(|args| { args[1] == "help" && match args[0].as_str() { @@ -368,15 +365,9 @@ pub fn main() { // this conditional check for the --sysroot flag is there so users can call // `clippy_driver` directly // without having to pass --sysroot or anything - let mut args: Vec = if have_sys_root_arg { - orig_args.clone() - } else { - orig_args - .clone() - .into_iter() - .chain(Some("--sysroot".to_owned())) - .chain(Some(sys_root)) - .collect() + let mut args: Vec = orig_args.clone(); + if !have_sys_root_arg { + args.extend(vec!["--sysroot".into(), sys_root]); }; // this check ensures that dependencies are built but not linted and the final @@ -385,7 +376,7 @@ pub fn main() { || arg_value(&orig_args, "--cap-lints", |val| val == "allow").is_none(); if clippy_enabled { - args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]); + args.extend(vec!["--cfg".into(), r#"feature="cargo-clippy""#.into()]); if let Ok(extra_args) = env::var("CLIPPY_ARGS") { args.extend(extra_args.split("__CLIPPY_HACKERY__").filter_map(|s| { if s.is_empty() { @@ -396,12 +387,10 @@ pub fn main() { })); } } - let mut clippy = ClippyCallbacks; let mut default = rustc_driver::DefaultCallbacks; let callbacks: &mut (dyn rustc_driver::Callbacks + Send) = if clippy_enabled { &mut clippy } else { &mut default }; - let args = args; rustc_driver::run_compiler(&args, callbacks, None, None) }) .and_then(|result| result) diff --git a/tests/integration.rs b/tests/integration.rs index 455965436d6..d14ced8ad4e 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,7 +1,6 @@ #![cfg(feature = "integration")] use git2::Repository; -use tempfile; use std::env; use std::process::Command; diff --git a/tests/ui/inefficient_to_string.fixed b/tests/ui/inefficient_to_string.fixed index 32bc7574a52..c972b9419ef 100644 --- a/tests/ui/inefficient_to_string.fixed +++ b/tests/ui/inefficient_to_string.fixed @@ -2,7 +2,6 @@ #![deny(clippy::inefficient_to_string)] use std::borrow::Cow; -use std::string::ToString; fn main() { let rstr: &str = "hello"; diff --git a/tests/ui/inefficient_to_string.rs b/tests/ui/inefficient_to_string.rs index 2741565e50d..acdc55aa0d6 100644 --- a/tests/ui/inefficient_to_string.rs +++ b/tests/ui/inefficient_to_string.rs @@ -2,7 +2,6 @@ #![deny(clippy::inefficient_to_string)] use std::borrow::Cow; -use std::string::ToString; fn main() { let rstr: &str = "hello"; diff --git a/tests/ui/inefficient_to_string.stderr b/tests/ui/inefficient_to_string.stderr index 20788f6f6cc..08592e7d588 100644 --- a/tests/ui/inefficient_to_string.stderr +++ b/tests/ui/inefficient_to_string.stderr @@ -1,5 +1,5 @@ error: calling `to_string` on `&&str` - --> $DIR/inefficient_to_string.rs:12:21 + --> $DIR/inefficient_to_string.rs:11:21 | LL | let _: String = rrstr.to_string(); | ^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(*rrstr).to_string()` @@ -12,7 +12,7 @@ LL | #![deny(clippy::inefficient_to_string)] = help: `&str` implements `ToString` through a slower blanket impl, but `str` has a fast specialization of `ToString` error: calling `to_string` on `&&&str` - --> $DIR/inefficient_to_string.rs:13:21 + --> $DIR/inefficient_to_string.rs:12:21 | LL | let _: String = rrrstr.to_string(); | ^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(**rrrstr).to_string()` @@ -20,7 +20,7 @@ LL | let _: String = rrrstr.to_string(); = help: `&&str` implements `ToString` through a slower blanket impl, but `str` has a fast specialization of `ToString` error: calling `to_string` on `&&std::string::String` - --> $DIR/inefficient_to_string.rs:21:21 + --> $DIR/inefficient_to_string.rs:20:21 | LL | let _: String = rrstring.to_string(); | ^^^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(*rrstring).to_string()` @@ -28,7 +28,7 @@ LL | let _: String = rrstring.to_string(); = help: `&std::string::String` implements `ToString` through a slower blanket impl, but `std::string::String` has a fast specialization of `ToString` error: calling `to_string` on `&&&std::string::String` - --> $DIR/inefficient_to_string.rs:22:21 + --> $DIR/inefficient_to_string.rs:21:21 | LL | let _: String = rrrstring.to_string(); | ^^^^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(**rrrstring).to_string()` @@ -36,7 +36,7 @@ LL | let _: String = rrrstring.to_string(); = help: `&&std::string::String` implements `ToString` through a slower blanket impl, but `std::string::String` has a fast specialization of `ToString` error: calling `to_string` on `&&std::borrow::Cow<'_, str>` - --> $DIR/inefficient_to_string.rs:30:21 + --> $DIR/inefficient_to_string.rs:29:21 | LL | let _: String = rrcow.to_string(); | ^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(*rrcow).to_string()` @@ -44,7 +44,7 @@ LL | let _: String = rrcow.to_string(); = help: `&std::borrow::Cow<'_, str>` implements `ToString` through a slower blanket impl, but `std::borrow::Cow<'_, str>` has a fast specialization of `ToString` error: calling `to_string` on `&&&std::borrow::Cow<'_, str>` - --> $DIR/inefficient_to_string.rs:31:21 + --> $DIR/inefficient_to_string.rs:30:21 | LL | let _: String = rrrcow.to_string(); | ^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(**rrrcow).to_string()`