Skip to content

fix clippy warnings #4061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions rustfmt-core/rustfmt-bin/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl CliOptions for Opt {
}

fn config_path(&self) -> Option<&Path> {
self.config_path.as_ref().map(PathBuf::as_path)
self.config_path.as_deref()
}
}

Expand Down Expand Up @@ -372,10 +372,7 @@ fn print_default_config() -> Result<i32> {
}

fn print_config(opt: &Opt, print_config: PrintConfig) -> Result<i32> {
let (config, config_path) = load_config(
env::current_dir().ok().as_ref().map(PathBuf::as_path),
Some(opt),
)?;
let (config, config_path) = load_config(env::current_dir().ok().as_deref(), Some(opt))?;
let actual_config =
FileConfigPairIter::new(&opt, config_path.is_some()).find_map(|pair| match pair.config {
FileConfig::Local(config, Some(_)) => Some(config),
Expand Down
5 changes: 4 additions & 1 deletion rustfmt-core/rustfmt-bin/tests/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,8 @@ fn print_config() {
fn rustfmt_help() {
assert_that!(&["--", "--help"], contains("Format Rust code"));
assert_that!(&["--", "-h"], contains("Format Rust code"));
assert_that!(&["--", "--config", "help"], contains("Configuration Options:"));
assert_that!(
&["--", "--config", "help"],
contains("Configuration Options:")
);
}
15 changes: 12 additions & 3 deletions rustfmt-core/rustfmt-bin/tests/rustfmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ fn print_config() {
);
assert_that!(&["--print-config", "default"], contains("max_width = 100"));
assert_that!(&["--print-config", "current"], contains("max_width = 100"));
assert_that!(&["--config", "max_width=120", "--print-config", "current"], contains("max_width = 120"));
assert_that!(&["--config", "max_width=120", "--print-config", "minimal"], contains("max_width = 120"));
assert_that!(&["--config", "max_width=100", "--print-config", "minimal"], eq(""));
assert_that!(
&["--config", "max_width=120", "--print-config", "current"],
contains("max_width = 120")
);
assert_that!(
&["--config", "max_width=120", "--print-config", "minimal"],
contains("max_width = 120")
);
assert_that!(
&["--config", "max_width=100", "--print-config", "minimal"],
eq("")
);
}

#[ignore]
Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-config/src/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl fmt::Display for FileLines {
Some(map) => {
for (file_name, ranges) in map.iter() {
write!(f, "{}: ", file_name)?;
write!(f, "{}\n", ranges.iter().format(", "))?;
writeln!(f, "{}", ranges.iter().format(", "))?;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-emitter/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Emitter for DiffEmitter {
return Ok(EmitterResult { has_diff: true });
}

return Ok(EmitterResult { has_diff });
Ok(EmitterResult { has_diff })
}
}

Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-lib/src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ fn is_block_closure_forced(
} else {
if let ast::ExprKind::Match(..) = expr.kind {
let is_move_closure_without_brace = capture == ast::CaptureBy::Value
&& !context.snippet(expr.span).trim().starts_with("{");
&& !context.snippet(expr.span).trim().starts_with('{');

is_block_closure_forced_inner(expr) || is_move_closure_without_brace
} else {
Expand Down
12 changes: 4 additions & 8 deletions rustfmt-core/rustfmt-lib/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,8 @@ impl UseTree {

// Normalise foo::self as bar -> foo as bar.
if let UseSegment::Slf(_) = last {
match self.path.last() {
Some(UseSegment::Ident(_, None)) => {
aliased_self = true;
}
_ => {}
if let Some(UseSegment::Ident(_, None)) = self.path.last() {
aliased_self = true;
}
}

Expand Down Expand Up @@ -540,9 +537,8 @@ impl UseTree {
match self.path.clone().last() {
Some(UseSegment::List(list)) => {
if list.len() == 1 && list[0].path.len() == 1 {
match list[0].path[0] {
UseSegment::Slf(..) => return vec![self],
_ => (),
if let UseSegment::Slf(..) = list[0].path[0] {
return vec![self];
};
}
let prefix = &self.path[..self.path.len() - 1];
Expand Down
12 changes: 3 additions & 9 deletions rustfmt-core/rustfmt-lib/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl<'a> FnSig<'a> {
result.push_str(&*format_visibility(context, &self.visibility));
result.push_str(format_defaultness(self.defaultness));
result.push_str(format_constness(self.constness));
result.push_str(format_async(&self.is_async));
result.push_str(format_async(*self.is_async));
result.push_str(format_unsafety(self.unsafety));
result.push_str(&format_extern(
self.ext,
Expand Down Expand Up @@ -649,20 +649,14 @@ impl<'a> FmtVisitor<'a> {
fn is_type(ty: &Option<syntax::ptr::P<ast::Ty>>) -> bool {
match ty {
None => true,
Some(lty) => match lty.kind.opaque_top_hack() {
None => true,
Some(_) => false,
},
Some(lty) => lty.kind.opaque_top_hack().is_none(),
}
}

fn is_opaque(ty: &Option<syntax::ptr::P<ast::Ty>>) -> bool {
match ty {
None => false,
Some(lty) => match lty.kind.opaque_top_hack() {
None => false,
Some(_) => true,
},
Some(lty) => lty.kind.opaque_top_hack().is_some(),
}
}

Expand Down
6 changes: 2 additions & 4 deletions rustfmt-core/rustfmt-lib/src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ impl<'a> FmtVisitor<'a> {
let last_char = big_snippet
.chars()
.rev()
.skip_while(|rev_c| [' ', '\t'].contains(rev_c))
.next();
.find(|rev_c| ![' ', '\t'].contains(rev_c));

let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));

Expand Down Expand Up @@ -276,8 +275,7 @@ impl<'a> FmtVisitor<'a> {
match snippet[status.line_start..]
.chars()
// skip trailing whitespaces
.skip_while(|c| *c == ' ' || *c == '\t')
.next()
.find(|c| !(*c == ' ' || *c == '\t'))
{
Some('\n') | Some('\r') => {
if !is_last_comment_block(subslice) {
Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-lib/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl Rewrite for Pat {
if let Some(rw) = p.rewrite(context, shape) {
rw
} else {
format!("{}", context.snippet(p.span))
context.snippet(p.span).to_string()
}
})
.collect();
Expand Down
14 changes: 5 additions & 9 deletions rustfmt-core/rustfmt-lib/src/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ impl SkipContext {
}
}
for attr in attrs {
match &attr.kind {
syntax::ast::AttrKind::Normal(ref attr_item) => {
if is_skip_attr_with(&attr_item.path.segments, |s| s == sym!(macros)) {
get_skip_names(&mut self.macros, attr)
} else if is_skip_attr_with(&attr_item.path.segments, |s| s == sym::attributes)
{
get_skip_names(&mut self.attributes, attr)
}
if let syntax::ast::AttrKind::Normal(ref attr_item) = &attr.kind {
if is_skip_attr_with(&attr_item.path.segments, |s| s == sym!(macros)) {
get_skip_names(&mut self.macros, attr)
} else if is_skip_attr_with(&attr_item.path.segments, |s| s == sym::attributes) {
get_skip_names(&mut self.attributes, attr)
}
_ => (),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-lib/src/syntux/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl<'a> Parser<'a> {
let token_stream = mac.args.inner_tokens();
let mut parser = rustc_parse::stream_to_parser_with_base_dir(
sess.inner(),
token_stream.clone(),
token_stream,
base_dir.to_syntax_directory(),
);

Expand Down
5 changes: 2 additions & 3 deletions rustfmt-core/rustfmt-lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub(crate) fn format_visibility(
}

#[inline]
pub(crate) fn format_async(is_async: &ast::IsAsync) -> &'static str {
pub(crate) fn format_async(is_async: ast::IsAsync) -> &'static str {
match is_async {
ast::IsAsync::Async { .. } => "async ",
ast::IsAsync::NotAsync => "",
Expand Down Expand Up @@ -255,8 +255,7 @@ fn is_skip(meta_item: &MetaItem) -> bool {
match meta_item.kind {
MetaItemKind::Word => {
let path_str = pprust::path_to_string(&meta_item.path);
path_str == &*skip_annotation().as_str()
|| path_str == &*depr_skip_annotation().as_str()
path_str == *skip_annotation().as_str() || path_str == *depr_skip_annotation().as_str()
}
MetaItemKind::List(ref l) => {
meta_item.check_name(sym::cfg_attr) && l.len() == 2 && is_skip_nested(&l[1])
Expand Down
4 changes: 2 additions & 2 deletions rustfmt-core/rustfmt-lib/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
let missing_span = self.next_span(hi);
let snippet = self.snippet(missing_span);
let len = CommentCodeSlices::new(snippet)
.nth(0)
.next()
.and_then(|(kind, _, s)| {
if kind == CodeCharKind::Normal {
s.rfind('\n')
Expand Down Expand Up @@ -690,7 +690,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
let hi = self.snippet_provider.span_before(search_span, ";");
let target_span = mk_sp(mac.span().lo(), hi + BytePos(1));
let rewrite = rewrite.map(|rw| {
if !rw.ends_with(";") {
if !rw.ends_with(';') {
format!("{};", rw)
} else {
rw
Expand Down