Skip to content

Commit f5b8964

Browse files
committed
do minor cleanups
* ToString and AsRef are in prelude, no need to import them
1 parent 37b7970 commit f5b8964

17 files changed

+45
-74
lines changed

clippy_dev/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Lint {
5050
name: name.to_lowercase(),
5151
group: group.to_string(),
5252
desc: NL_ESCAPE_RE.replace(&desc.replace("\\\"", "\""), "").to_string(),
53-
deprecation: deprecation.map(std::string::ToString::to_string),
53+
deprecation: deprecation.map(ToString::to_string),
5454
module: module.to_string(),
5555
}
5656
}

clippy_lints/src/cargo_common_metadata.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
88
use rustc_session::declare_tool_lint;
99
use syntax::{ast::*, source_map::DUMMY_SP};
1010

11-
use cargo_metadata;
12-
1311
declare_clippy_lint! {
1412
/// **What it does:** Checks to see if all common metadata is defined in
1513
/// `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 {
5654

5755
fn is_empty_vec(value: &[String]) -> bool {
5856
// This works because empty iterators return true
59-
value.iter().all(std::string::String::is_empty)
57+
value.iter().all(String::is_empty)
6058
}
6159

6260
declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]);

clippy_lints/src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
253253
let res = self.tables.qpath_res(qpath, callee.hir_id);
254254
if let Some(def_id) = res.opt_def_id();
255255
let def_path: Vec<_> = self.lcx.get_def_path(def_id).into_iter().map(Symbol::as_str).collect();
256-
let def_path: Vec<&str> = def_path.iter().map(|s| &**s).collect();
256+
let def_path: Vec<&str> = def_path.iter().take(4).map(|s| &**s).collect();
257257
if let ["core", "num", int_impl, "max_value"] = *def_path;
258258
then {
259259
let value = match int_impl {

clippy_lints/src/doc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::utils::{match_type, paths, return_ty, span_lint};
22
use itertools::Itertools;
3-
use pulldown_cmark;
43
use rustc::hir;
54
use rustc::impl_lint_pass;
65
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};

clippy_lints/src/eta_reduction.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,7 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) {
146146
}
147147

148148
/// Tries to determine the type for universal function call to be used instead of the closure
149-
fn get_ufcs_type_name(
150-
cx: &LateContext<'_, '_>,
151-
method_def_id: def_id::DefId,
152-
self_arg: &Expr,
153-
) -> std::option::Option<String> {
149+
fn get_ufcs_type_name(cx: &LateContext<'_, '_>, method_def_id: def_id::DefId, self_arg: &Expr) -> Option<String> {
154150
let expected_type_of_self = &cx.tcx.fn_sig(method_def_id).inputs_and_output().skip_binder()[0];
155151
let actual_type_of_self = &cx.tables.node_type(self_arg.hir_id);
156152

clippy_lints/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ extern crate syntax_pos;
4747
use rustc::lint::{self, LintId};
4848
use rustc::session::Session;
4949
use rustc_data_structures::fx::FxHashSet;
50-
use toml;
50+
51+
use std::path::Path;
5152

5253
/// Macro used to declare a Clippy lint.
5354
///
@@ -349,16 +350,16 @@ pub fn read_conf(args: &[syntax::ast::NestedMetaItem], sess: &Session) -> Conf {
349350
let file_name = file_name.map(|file_name| {
350351
if file_name.is_relative() {
351352
sess.local_crate_source_file
352-
.as_ref()
353-
.and_then(|file| std::path::Path::new(&file).parent().map(std::path::Path::to_path_buf))
354-
.unwrap_or_default()
353+
.as_deref()
354+
.and_then(Path::parent)
355+
.unwrap_or_else(|| Path::new(""))
355356
.join(file_name)
356357
} else {
357358
file_name
358359
}
359360
});
360361

361-
let (conf, errors) = utils::conf::read(file_name.as_ref().map(std::convert::AsRef::as_ref));
362+
let (conf, errors) = utils::conf::read(file_name.as_ref().map(AsRef::as_ref));
362363

363364
// all conf errors are non-fatal, we just use the default conf in case of error
364365
for error in errors {

clippy_lints/src/literal_representation.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc::{declare_lint_pass, impl_lint_pass};
88
use rustc_errors::Applicability;
99
use rustc_session::declare_tool_lint;
1010
use syntax::ast::*;
11-
use syntax_pos;
1211

1312
declare_clippy_lint! {
1413
/// **What it does:** Warns if a long integral or floating-point constant does

clippy_lints/src/multiple_crate_versions.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
66
use rustc_session::declare_tool_lint;
77
use syntax::{ast::*, source_map::DUMMY_SP};
88

9-
use cargo_metadata;
109
use itertools::Itertools;
1110

1211
declare_clippy_lint! {

clippy_lints/src/regex.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::consts::{constant, Constant};
22
use crate::utils::{is_expn_of, match_def_path, match_type, paths, span_help_and_lint, span_lint};
33
use if_chain::if_chain;
4-
use regex_syntax;
54
use rustc::hir::*;
65
use rustc::impl_lint_pass;
76
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};

clippy_lints/src/utils/conf.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::io::Read;
88
use std::sync::Mutex;
99
use std::{env, fmt, fs, io, path};
1010
use syntax::{ast, source_map};
11-
use toml;
1211

1312
/// Gets the configuration file from arguments.
1413
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 {
7776
}
7877
$(
7978
mod $rust_name {
80-
use serde;
8179
use serde::Deserialize;
8280
crate fn deserialize<'de, D: serde::Deserializer<'de>>(deserializer: D)
8381
-> Result<define_Conf!(TY $($ty)+), D::Error> {

clippy_lints/src/utils/sugg.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use crate::utils::{higher, snippet, snippet_opt, snippet_with_macro_callsite};
55
use matches::matches;
66
use rustc::hir;
77
use rustc::lint::{EarlyContext, LateContext, LintContext};
8-
use rustc_errors;
98
use rustc_errors::Applicability;
10-
use std;
119
use std::borrow::Cow;
1210
use std::convert::TryInto;
1311
use std::fmt::Display;

clippy_lints/src/wildcard_dependencies.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
44
use rustc_session::declare_tool_lint;
55
use syntax::{ast::*, source_map::DUMMY_SP};
66

7-
use cargo_metadata;
87
use if_chain::if_chain;
9-
use semver;
108

119
declare_clippy_lint! {
1210
/// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.

src/driver.rs

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
22
#![feature(rustc_private)]
3+
#![feature(str_strip)]
34

45
// FIXME: switch to something more ergonomic here, once available.
56
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
@@ -18,6 +19,8 @@ use rustc_tools_util::*;
1819

1920
use lazy_static::lazy_static;
2021
use std::borrow::Cow;
22+
use std::env;
23+
use std::ops::Deref;
2124
use std::panic;
2225
use std::path::{Path, PathBuf};
2326
use std::process::{exit, Command};
@@ -26,42 +29,38 @@ mod lintlist;
2629

2730
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
2831
/// true, then return it. The parameter is assumed to be either `--arg=value` or `--arg value`.
29-
fn arg_value<'a>(
30-
args: impl IntoIterator<Item = &'a String>,
32+
fn arg_value<'a, T: Deref<Target = str>>(
33+
args: &'a [T],
3134
find_arg: &str,
3235
pred: impl Fn(&str) -> bool,
3336
) -> Option<&'a str> {
34-
let mut args = args.into_iter().map(String::as_str);
35-
37+
let mut args = args.iter().map(Deref::deref);
3638
while let Some(arg) = args.next() {
37-
let arg: Vec<_> = arg.splitn(2, '=').collect();
38-
if arg.get(0) != Some(&find_arg) {
39+
let mut arg = arg.splitn(2, '=');
40+
if arg.next() != Some(find_arg) {
3941
continue;
4042
}
4143

42-
let value = arg.get(1).cloned().or_else(|| args.next());
43-
if value.as_ref().map_or(false, |p| pred(p)) {
44-
return value;
44+
match arg.next().or_else(|| args.next()) {
45+
Some(v) if pred(v) => return Some(v),
46+
_ => {},
4547
}
4648
}
4749
None
4850
}
4951

5052
#[test]
5153
fn test_arg_value() {
52-
let args: Vec<_> = ["--bar=bar", "--foobar", "123", "--foo"]
53-
.iter()
54-
.map(std::string::ToString::to_string)
55-
.collect();
56-
57-
assert_eq!(arg_value(None, "--foobar", |_| true), None);
58-
assert_eq!(arg_value(&args, "--bar", |_| false), None);
59-
assert_eq!(arg_value(&args, "--bar", |_| true), Some("bar"));
60-
assert_eq!(arg_value(&args, "--bar", |p| p == "bar"), Some("bar"));
61-
assert_eq!(arg_value(&args, "--bar", |p| p == "foo"), None);
62-
assert_eq!(arg_value(&args, "--foobar", |p| p == "foo"), None);
63-
assert_eq!(arg_value(&args, "--foobar", |p| p == "123"), Some("123"));
64-
assert_eq!(arg_value(&args, "--foo", |_| true), None);
54+
let args = &["--bar=bar", "--foobar", "123", "--foo"];
55+
56+
assert_eq!(arg_value(&[] as &[&str], "--foobar", |_| true), None);
57+
assert_eq!(arg_value(args, "--bar", |_| false), None);
58+
assert_eq!(arg_value(args, "--bar", |_| true), Some("bar"));
59+
assert_eq!(arg_value(args, "--bar", |p| p == "bar"), Some("bar"));
60+
assert_eq!(arg_value(args, "--bar", |p| p == "foo"), None);
61+
assert_eq!(arg_value(args, "--foobar", |p| p == "foo"), None);
62+
assert_eq!(arg_value(args, "--foobar", |p| p == "123"), Some("123"));
63+
assert_eq!(arg_value(args, "--foo", |_| true), None);
6564
}
6665

6766
#[allow(clippy::too_many_lines)]
@@ -276,7 +275,7 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
276275
}
277276

278277
// If backtraces are enabled, also print the query stack
279-
let backtrace = std::env::var_os("RUST_BACKTRACE").map_or(false, |x| &x != "0");
278+
let backtrace = env::var_os("RUST_BACKTRACE").map_or(false, |x| &x != "0");
280279

281280
if backtrace {
282281
TyCtxt::try_print_query_stack(&handler);
@@ -288,16 +287,14 @@ pub fn main() {
288287
lazy_static::initialize(&ICE_HOOK);
289288
exit(
290289
rustc_driver::catch_fatal_errors(move || {
291-
use std::env;
290+
let mut orig_args: Vec<String> = env::args().collect();
292291

293-
if std::env::args().any(|a| a == "--version" || a == "-V") {
292+
if orig_args.iter().any(|a| a == "--version" || a == "-V") {
294293
let version_info = rustc_tools_util::get_version_info!();
295294
println!("{}", version_info);
296295
exit(0);
297296
}
298297

299-
let mut orig_args: Vec<String> = env::args().collect();
300-
301298
// Get the sysroot, looking from most specific to this invocation to the least:
302299
// - command line
303300
// - runtime environment
@@ -350,7 +347,7 @@ pub fn main() {
350347
}
351348

352349
let should_describe_lints = || {
353-
let args: Vec<_> = std::env::args().collect();
350+
let args: Vec<_> = env::args().collect();
354351
args.windows(2).any(|args| {
355352
args[1] == "help"
356353
&& match args[0].as_str() {
@@ -368,15 +365,9 @@ pub fn main() {
368365
// this conditional check for the --sysroot flag is there so users can call
369366
// `clippy_driver` directly
370367
// without having to pass --sysroot or anything
371-
let mut args: Vec<String> = if have_sys_root_arg {
372-
orig_args.clone()
373-
} else {
374-
orig_args
375-
.clone()
376-
.into_iter()
377-
.chain(Some("--sysroot".to_owned()))
378-
.chain(Some(sys_root))
379-
.collect()
368+
let mut args: Vec<String> = orig_args.clone();
369+
if !have_sys_root_arg {
370+
args.extend(vec!["--sysroot".into(), sys_root]);
380371
};
381372

382373
// this check ensures that dependencies are built but not linted and the final
@@ -385,7 +376,7 @@ pub fn main() {
385376
|| arg_value(&orig_args, "--cap-lints", |val| val == "allow").is_none();
386377

387378
if clippy_enabled {
388-
args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
379+
args.extend(vec!["--cfg".into(), r#"feature="cargo-clippy""#.into()]);
389380
if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
390381
args.extend(extra_args.split("__CLIPPY_HACKERY__").filter_map(|s| {
391382
if s.is_empty() {
@@ -396,12 +387,10 @@ pub fn main() {
396387
}));
397388
}
398389
}
399-
400390
let mut clippy = ClippyCallbacks;
401391
let mut default = rustc_driver::DefaultCallbacks;
402392
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) =
403393
if clippy_enabled { &mut clippy } else { &mut default };
404-
let args = args;
405394
rustc_driver::run_compiler(&args, callbacks, None, None)
406395
})
407396
.and_then(|result| result)

tests/integration.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![cfg(feature = "integration")]
22

33
use git2::Repository;
4-
use tempfile;
54

65
use std::env;
76
use std::process::Command;

tests/ui/inefficient_to_string.fixed

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![deny(clippy::inefficient_to_string)]
33

44
use std::borrow::Cow;
5-
use std::string::ToString;
65

76
fn main() {
87
let rstr: &str = "hello";

tests/ui/inefficient_to_string.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![deny(clippy::inefficient_to_string)]
33

44
use std::borrow::Cow;
5-
use std::string::ToString;
65

76
fn main() {
87
let rstr: &str = "hello";

tests/ui/inefficient_to_string.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: calling `to_string` on `&&str`
2-
--> $DIR/inefficient_to_string.rs:12:21
2+
--> $DIR/inefficient_to_string.rs:11:21
33
|
44
LL | let _: String = rrstr.to_string();
55
| ^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(*rrstr).to_string()`
@@ -12,39 +12,39 @@ LL | #![deny(clippy::inefficient_to_string)]
1212
= help: `&str` implements `ToString` through a slower blanket impl, but `str` has a fast specialization of `ToString`
1313

1414
error: calling `to_string` on `&&&str`
15-
--> $DIR/inefficient_to_string.rs:13:21
15+
--> $DIR/inefficient_to_string.rs:12:21
1616
|
1717
LL | let _: String = rrrstr.to_string();
1818
| ^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(**rrrstr).to_string()`
1919
|
2020
= help: `&&str` implements `ToString` through a slower blanket impl, but `str` has a fast specialization of `ToString`
2121

2222
error: calling `to_string` on `&&std::string::String`
23-
--> $DIR/inefficient_to_string.rs:21:21
23+
--> $DIR/inefficient_to_string.rs:20:21
2424
|
2525
LL | let _: String = rrstring.to_string();
2626
| ^^^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(*rrstring).to_string()`
2727
|
2828
= help: `&std::string::String` implements `ToString` through a slower blanket impl, but `std::string::String` has a fast specialization of `ToString`
2929

3030
error: calling `to_string` on `&&&std::string::String`
31-
--> $DIR/inefficient_to_string.rs:22:21
31+
--> $DIR/inefficient_to_string.rs:21:21
3232
|
3333
LL | let _: String = rrrstring.to_string();
3434
| ^^^^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(**rrrstring).to_string()`
3535
|
3636
= help: `&&std::string::String` implements `ToString` through a slower blanket impl, but `std::string::String` has a fast specialization of `ToString`
3737

3838
error: calling `to_string` on `&&std::borrow::Cow<'_, str>`
39-
--> $DIR/inefficient_to_string.rs:30:21
39+
--> $DIR/inefficient_to_string.rs:29:21
4040
|
4141
LL | let _: String = rrcow.to_string();
4242
| ^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(*rrcow).to_string()`
4343
|
4444
= help: `&std::borrow::Cow<'_, str>` implements `ToString` through a slower blanket impl, but `std::borrow::Cow<'_, str>` has a fast specialization of `ToString`
4545

4646
error: calling `to_string` on `&&&std::borrow::Cow<'_, str>`
47-
--> $DIR/inefficient_to_string.rs:31:21
47+
--> $DIR/inefficient_to_string.rs:30:21
4848
|
4949
LL | let _: String = rrrcow.to_string();
5050
| ^^^^^^^^^^^^^^^^^^ help: try dereferencing the receiver: `(**rrrcow).to_string()`

0 commit comments

Comments
 (0)