do minor cleanups

* ToString and AsRef are in prelude, no need to import them
This commit is contained in:
Lzu Tao 2019-12-24 03:06:52 +07:00
parent 37b7970a7c
commit f5b896451a
17 changed files with 44 additions and 73 deletions

View file

@ -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(),
}
}

View file

@ -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<PathBuf>) -> 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]);

View file

@ -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 {

View file

@ -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};

View file

@ -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<String> {
fn get_ufcs_type_name(cx: &LateContext<'_, '_>, method_def_id: def_id::DefId, self_arg: &Expr) -> Option<String> {
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);

View file

@ -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 {

View file

@ -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

View file

@ -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! {

View file

@ -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};

View file

@ -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<Option<path::PathBuf>, (&'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<define_Conf!(TY $($ty)+), D::Error> {

View file

@ -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;

View file

@ -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`.

View file

@ -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<Item = &'a String>,
fn arg_value<'a, T: Deref<Target = str>>(
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<String> = 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<String> = 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<String> = 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<String> = 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)

View file

@ -1,7 +1,6 @@
#![cfg(feature = "integration")]
use git2::Repository;
use tempfile;
use std::env;
use std::process::Command;

View file

@ -2,7 +2,6 @@
#![deny(clippy::inefficient_to_string)]
use std::borrow::Cow;
use std::string::ToString;
fn main() {
let rstr: &str = "hello";

View file

@ -2,7 +2,6 @@
#![deny(clippy::inefficient_to_string)]
use std::borrow::Cow;
use std::string::ToString;
fn main() {
let rstr: &str = "hello";

View file

@ -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()`