Skip to content

Commit d08405e

Browse files
authoredAug 31, 2017
Merge pull request rust-lang#1931 from topecongiro/cargo-clippy
Apply refactoring from cargo clippy
2 parents c5a5db2 + 9d49bd2 commit d08405e

21 files changed

+185
-213
lines changed
 

‎src/bin/cargo-fmt.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn execute() -> i32 {
8484

8585
let workspace_hitlist = WorkspaceHitlist::from_matches(&matches);
8686

87-
match format_crate(verbosity, workspace_hitlist) {
87+
match format_crate(verbosity, &workspace_hitlist) {
8888
Err(e) => {
8989
print_usage(&opts, &e.to_string());
9090
failure
@@ -115,7 +115,7 @@ pub enum Verbosity {
115115

116116
fn format_crate(
117117
verbosity: Verbosity,
118-
workspace_hitlist: WorkspaceHitlist,
118+
workspace_hitlist: &WorkspaceHitlist,
119119
) -> Result<ExitStatus, std::io::Error> {
120120
let targets = get_targets(workspace_hitlist)?;
121121

@@ -178,9 +178,9 @@ pub enum WorkspaceHitlist {
178178
}
179179

180180
impl WorkspaceHitlist {
181-
pub fn get_some<'a>(&'a self) -> Option<&'a [String]> {
182-
if let &WorkspaceHitlist::Some(ref hitlist) = self {
183-
Some(&hitlist)
181+
pub fn get_some(&self) -> Option<&[String]> {
182+
if let WorkspaceHitlist::Some(ref hitlist) = *self {
183+
Some(hitlist)
184184
} else {
185185
None
186186
}
@@ -196,9 +196,9 @@ impl WorkspaceHitlist {
196196
}
197197

198198
// Returns a vector of all compile targets of a crate
199-
fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::io::Error> {
199+
fn get_targets(workspace_hitlist: &WorkspaceHitlist) -> Result<Vec<Target>, std::io::Error> {
200200
let mut targets: Vec<Target> = vec![];
201-
if workspace_hitlist == WorkspaceHitlist::None {
201+
if *workspace_hitlist == WorkspaceHitlist::None {
202202
let output = Command::new("cargo").arg("read-manifest").output()?;
203203
if output.status.success() {
204204
// None of the unwraps should fail if output of `cargo read-manifest` is correct
@@ -229,7 +229,7 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::
229229
let data = &String::from_utf8(output.stdout).unwrap();
230230
let json: Value = json::from_str(data).unwrap();
231231
let json_obj = json.as_object().unwrap();
232-
let mut hitlist: HashSet<&String> = if workspace_hitlist != WorkspaceHitlist::All {
232+
let mut hitlist: HashSet<&String> = if *workspace_hitlist != WorkspaceHitlist::All {
233233
HashSet::from_iter(workspace_hitlist.get_some().unwrap())
234234
} else {
235235
HashSet::new() // Unused
@@ -240,15 +240,15 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::
240240
.as_array()
241241
.unwrap()
242242
.into_iter()
243-
.filter(|member| if workspace_hitlist == WorkspaceHitlist::All {
243+
.filter(|member| if *workspace_hitlist == WorkspaceHitlist::All {
244244
true
245245
} else {
246246
let member_obj = member.as_object().unwrap();
247247
let member_name = member_obj.get("name").unwrap().as_str().unwrap();
248248
hitlist.take(&member_name.to_string()).is_some()
249249
})
250250
.collect();
251-
if hitlist.len() != 0 {
251+
if hitlist.is_empty() {
252252
// Mimick cargo of only outputting one <package> spec.
253253
return Err(std::io::Error::new(
254254
std::io::ErrorKind::InvalidInput,

‎src/bin/rustfmt.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn match_cli_path_or_file(
104104
let toml = Config::from_toml_path(config_file.as_ref())?;
105105
return Ok((toml, Some(config_file)));
106106
}
107-
Config::from_resolved_toml_path(input_file).map_err(|e| FmtError::from(e))
107+
Config::from_resolved_toml_path(input_file).map_err(FmtError::from)
108108
}
109109

110110
fn make_opts() -> Options {
@@ -161,21 +161,21 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
161161
Operation::Help => {
162162
print_usage(opts, "");
163163
Summary::print_exit_codes();
164-
Ok(Summary::new())
164+
Ok(Summary::default())
165165
}
166166
Operation::Version => {
167167
print_version();
168-
Ok(Summary::new())
168+
Ok(Summary::default())
169169
}
170170
Operation::ConfigHelp => {
171171
Config::print_docs();
172-
Ok(Summary::new())
172+
Ok(Summary::default())
173173
}
174174
Operation::ConfigOutputDefault { path } => {
175175
let mut file = File::create(path)?;
176176
let toml = Config::default().all_options().to_toml()?;
177177
file.write_all(toml.as_bytes())?;
178-
Ok(Summary::new())
178+
Ok(Summary::default())
179179
}
180180
Operation::Stdin { input, config_path } => {
181181
// try to read config from local directory
@@ -222,7 +222,7 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
222222
}
223223
}
224224

225-
let mut error_summary = Summary::new();
225+
let mut error_summary = Summary::default();
226226
for file in files {
227227
if !file.exists() {
228228
println!("Error: file `{}` does not exist", file.to_str().unwrap());
@@ -354,7 +354,7 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
354354
return config_path_not_found(path.to_str().unwrap());
355355
}
356356
}
357-
path @ _ => path,
357+
path => path,
358358
};
359359

360360
// If no path is given, we won't output a minimal config.

‎src/chains.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
256256
"{}{}{}",
257257
parent_rewrite,
258258
first_connector,
259-
join_rewrites(&rewrites, &subexpr_list, &connector)
259+
join_rewrites(&rewrites, subexpr_list, &connector)
260260
)
261261
};
262262
let result = format!("{}{}", result, repeat_try(suffix_try_num));
@@ -475,7 +475,7 @@ fn rewrite_method_call(
475475
let type_list: Vec<_> =
476476
try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect());
477477

478-
let type_str = if context.config.spaces_within_angle_brackets() && type_list.len() > 0 {
478+
let type_str = if context.config.spaces_within_angle_brackets() && !type_list.is_empty() {
479479
format!("::< {} >", type_list.join(", "))
480480
} else {
481481
format!("::<{}>", type_list.join(", "))

‎src/codemap.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! This module contains utilities that work with the `CodeMap` from libsyntax / syntex_syntax.
11+
//! This module contains utilities that work with the `CodeMap` from `libsyntax` / `syntex_syntax`.
1212
//! This includes extension traits and methods for looking up spans and line ranges for AST nodes.
1313
1414
use std::rc::Rc;
@@ -77,8 +77,9 @@ impl LineRangeUtils for CodeMap {
7777
let lo = self.lookup_char_pos(span.lo());
7878
let hi = self.lookup_char_pos(span.hi());
7979

80-
assert!(
81-
lo.file.name == hi.file.name,
80+
assert_eq!(
81+
lo.file.name,
82+
hi.file.name,
8283
"span crossed file boundary: lo: {:?}, hi: {:?}",
8384
lo,
8485
hi

‎src/comment.rs

+18-24
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ use utils::{first_line_width, last_line_width, wrap_str};
2323
fn is_custom_comment(comment: &str) -> bool {
2424
if !comment.starts_with("//") {
2525
false
26+
} else if let Some(c) = comment.chars().nth(2) {
27+
!c.is_alphanumeric() && !c.is_whitespace()
2628
} else {
27-
if let Some(c) = comment.chars().nth(2) {
28-
!c.is_alphanumeric() && !c.is_whitespace()
29-
} else {
30-
false
31-
}
29+
false
3230
}
3331
}
3432

35-
#[derive(PartialEq, Eq)]
33+
#[derive(Copy, Clone, PartialEq, Eq)]
3634
pub enum CommentStyle<'a> {
3735
DoubleSlash,
3836
TripleSlash,
@@ -194,17 +192,15 @@ pub fn combine_strs_with_missing_comments(
194192
};
195193
let second_sep = if missing_comment.is_empty() || next_str.is_empty() {
196194
String::new()
195+
} else if missing_comment.starts_with("//") {
196+
format!("\n{}", indent_str)
197197
} else {
198-
if missing_comment.starts_with("//") {
199-
format!("\n{}", indent_str)
198+
one_line_width += missing_comment.len() + first_sep.len() + 1;
199+
allow_one_line &= !missing_comment.starts_with("//") && !missing_comment.contains('\n');
200+
if prefer_same_line && allow_one_line && one_line_width <= shape.width {
201+
String::from(" ")
200202
} else {
201-
one_line_width += missing_comment.len() + first_sep.len() + 1;
202-
allow_one_line &= !missing_comment.starts_with("//") && !missing_comment.contains('\n');
203-
if prefer_same_line && allow_one_line && one_line_width <= shape.width {
204-
String::from(" ")
205-
} else {
206-
format!("\n{}", indent_str)
207-
}
203+
format!("\n{}", indent_str)
208204
}
209205
};
210206
Some(format!(
@@ -314,7 +310,7 @@ fn rewrite_comment_inner(
314310
line = line.trim();
315311
// Drop old closer.
316312
if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
317-
line = &line[..(line.len() - 2)].trim_right();
313+
line = line[..(line.len() - 2)].trim_right();
318314
}
319315

320316
line
@@ -339,7 +335,7 @@ fn rewrite_comment_inner(
339335
}
340336

341337
if config.wrap_comments() && line.len() > max_chars {
342-
let rewrite = rewrite_string(line, &fmt).unwrap_or(line.to_owned());
338+
let rewrite = rewrite_string(line, &fmt).unwrap_or_else(|| line.to_owned());
343339
result.push_str(&rewrite);
344340
} else {
345341
if line.is_empty() && result.ends_with(' ') {
@@ -412,7 +408,7 @@ fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option<
412408
// `*` in `/*`.
413409
let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
414410
if let Some(fnw) = first_non_whitespace {
415-
if l.as_bytes()[fnw] == '*' as u8 && fnw > 0 {
411+
if l.as_bytes()[fnw] == b'*' && fnw > 0 {
416412
&l[fnw - 1..]
417413
} else {
418414
&l[fnw..]
@@ -432,7 +428,7 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> &'a str {
432428
line.starts_with("/** ")
433429
{
434430
&line[4..]
435-
} else if let &CommentStyle::Custom(opener) = style {
431+
} else if let CommentStyle::Custom(opener) = *style {
436432
if line.starts_with(opener) {
437433
&line[opener.len()..]
438434
} else {
@@ -646,7 +642,7 @@ where
646642
_ => CharClassesStatus::Normal,
647643
},
648644
CharClassesStatus::BlockComment(deepness) => {
649-
assert!(deepness != 0);
645+
assert_ne!(deepness, 0);
650646
self.status = match self.base.peek() {
651647
Some(next) if next.get_char() == '/' && chr == '*' => {
652648
CharClassesStatus::BlockCommentClosing(deepness - 1)
@@ -901,10 +897,8 @@ impl<'a> Iterator for CommentReducer<'a> {
901897
if c == '*' {
902898
c = try_opt!(self.iter.next());
903899
}
904-
} else {
905-
if c == '\n' {
906-
self.at_start_line = true;
907-
}
900+
} else if c == '\n' {
901+
self.at_start_line = true;
908902
}
909903
if !c.is_whitespace() {
910904
return Some(c);

‎src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ macro_rules! create_config {
306306
let table = parsed
307307
.as_table()
308308
.ok_or(String::from("Parsed config was not table"))?;
309-
for (key, _) in table {
309+
for key in table.keys() {
310310
match &**key {
311311
$(
312312
stringify!($i) => (),

0 commit comments

Comments
 (0)
Please sign in to comment.