Skip to content

Commit

Permalink
Various cosmetic improvements (#3403)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Regueiro authored and topecongiro committed Feb 19, 2019
1 parent 74755e8 commit 7a3b7c9
Show file tree
Hide file tree
Showing 63 changed files with 198 additions and 603 deletions.
10 changes: 0 additions & 10 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,13 1,3 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::env;
use std::fs::File;
use std::io::Write;
Expand Down
10 changes: 0 additions & 10 deletions src/attr.rs
Original file line number Diff line number Diff line change
@@ -1,13 1,3 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Format attributes and meta items.

use syntax::ast;
Expand Down
12 changes: 1 addition & 11 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 1,3 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use env_logger;
#[macro_use]
extern crate failure;
Expand Down Expand Up @@ -45,7 35,7 @@ fn main() {

// Exit with given exit code.
//
// NOTE: This immediately terminates the process without doing any cleanup,
// NOTE: this immediately terminates the process without doing any cleanup,
// so make sure to finish all necessary cleanup before this is called.
std::process::exit(exit_code);
}
Expand Down
18 changes: 4 additions & 14 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 1,4 @@
// Copyright 2015-2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Inspired by Paul Woolcock's cargo-fmt (https://github.com/pwoolcoc/cargo-fmt/)
// Inspired by Paul Woolcock's cargo-fmt (https://github.com/pwoolcoc/cargo-fmt/).

#![cfg(not(test))]
#![deny(warnings)]
Expand Down Expand Up @@ -146,12 136,12 @@ fn format_crate(verbosity: Verbosity, strategy: &CargoFmtStrategy) -> Result<i32
get_targets(strategy)?
};

// Currently only bin and lib files get formatted
// Currently only bin and lib files get formatted.
run_rustfmt(&targets, &rustfmt_args, verbosity)
}

fn get_fmt_args() -> Vec<String> {
// All arguments after -- are passed to rustfmt
// All arguments after -- are passed to rustfmt.
env::args().skip_while(|a| a != "--").skip(1).collect()
}

Expand All @@ -160,7 150,7 @@ fn get_fmt_args() -> Vec<String> {
pub struct Target {
/// A path to the main source file of the target.
path: PathBuf,
/// A kind of target (e.g. lib, bin, example, ...).
/// A kind of target (e.g., lib, bin, example, ...).
kind: String,
/// Rust edition for this target.
edition: String,
Expand Down
18 changes: 4 additions & 14 deletions src/chains.rs
Original file line number Diff line number Diff line change
@@ -1,15 1,5 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Formatting of chained expressions, i.e. expressions which are chained by
//! dots: struct and enum field access, method calls, and try shorthand (?).
//! Formatting of chained expressions, i.e., expressions that are chained by
//! dots: struct and enum field access, method calls, and try shorthand (`?`).
//!
//! Instead of walking these subexpressions one-by-one, as is our usual strategy
//! for expression formatting, we collect maximal sequences of these expressions
Expand Down Expand Up @@ -534,7 524,7 @@ impl<'a> ChainFormatterShared<'a> {
// ```
//
// In particular, overflowing is effective when the last child is a method with a multi-lined
// block-like argument (e.g. closure):
// block-like argument (e.g., closure):
// ```ignore
// parent.child1.child2.last_child(|a, b, c| {
// let x = foo(a, b, c);
Expand Down Expand Up @@ -853,7 843,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
}
}

/// Remove try operators (`?`s) that appear in the given string. If removing
/// Removes try operators (`?`s) that appear in the given string. If removing
/// them leaves an empty line, remove that line as well unless it is the first
/// line (we need the first newline for detecting pre/post comment).
fn trim_tries(s: &str) -> String {
Expand Down
10 changes: 0 additions & 10 deletions src/checkstyle.rs
Original file line number Diff line number Diff line change
@@ -1,13 1,3 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::io::{self, Write};
use std::path::Path;

Expand Down
12 changes: 1 addition & 11 deletions src/closures.rs
Original file line number Diff line number Diff line change
@@ -1,13 1,3 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use syntax::parse::classify;
use syntax::source_map::Span;
use syntax::{ast, ptr};
Expand Down Expand Up @@ -360,7 350,7 @@ pub fn rewrite_last_closure(
None
}

/// Returns true if the given vector of arguments has more than one `ast::ExprKind::Closure`.
/// Returns `true` if the given vector of arguments has more than one `ast::ExprKind::Closure`.
pub fn args_have_many_closure(args: &[OverflowableItem<'_>]) -> bool {
args.iter()
.filter_map(|arg| arg.to_expr())
Expand Down
67 changes: 29 additions & 38 deletions src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,13 1,3 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Formatting and tools for comments.

use std::{self, borrow::Cow, iter};
Expand Down Expand Up @@ -54,7 44,7 @@ fn custom_opener(s: &str) -> &str {
}

impl<'a> CommentStyle<'a> {
/// Returns true if the commenting style covers a line only.
/// Returns `true` if the commenting style covers a line only.
pub fn is_line_comment(&self) -> bool {
match *self {
CommentStyle::DoubleSlash
Expand All @@ -65,7 55,7 @@ impl<'a> CommentStyle<'a> {
}
}

/// Returns true if the commenting style can span over multiple lines.
/// Returns `true` if the commenting style can span over multiple lines.
pub fn is_block_comment(&self) -> bool {
match *self {
CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
Expand All @@ -75,7 65,7 @@ impl<'a> CommentStyle<'a> {
}
}

/// Returns true if the commenting style is for documentation.
/// Returns `true` if the commenting style is for documentation.
pub fn is_doc_comment(&self) -> bool {
match *self {
CommentStyle::TripleSlash | CommentStyle::Doc => true,
Expand Down Expand Up @@ -439,7 429,7 @@ struct ItemizedBlock {
}

impl ItemizedBlock {
/// Returns true if the line is formatted as an item
/// Returns `true` if the line is formatted as an item
fn is_itemized_line(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.starts_with("* ") || trimmed.starts_with("- ")
Expand All @@ -458,7 448,7 @@ impl ItemizedBlock {
}
}

/// Returns a `StringFormat` used for formatting the content of an item
/// Returns a `StringFormat` used for formatting the content of an item.
fn create_string_format<'a>(&'a self, fmt: &'a StringFormat<'_>) -> StringFormat<'a> {
StringFormat {
opener: "",
Expand All @@ -471,8 461,8 @@ impl ItemizedBlock {
}
}

/// Returns true if the line is part of the current itemized block.
/// If it is, then it is added to the internal lines vec.
/// Returns `true` if the line is part of the current itemized block.
/// If it is, then it is added to the internal lines list.
fn add_line(&mut self, line: &str) -> bool {
if !ItemizedBlock::is_itemized_line(line)
&& self.indent <= line.chars().take_while(|c| c.is_whitespace()).count()
Expand All @@ -491,7 481,7 @@ impl ItemizedBlock {
.collect::<String>()
}

/// Returns the block as a string under its original form
/// Returns the block as a string under its original form.
fn original_block_as_string(&self) -> String {
self.lines.join("\n")
}
Expand Down Expand Up @@ -842,7 832,7 @@ fn trim_custom_comment_prefix(s: &str) -> String {
.join("\n")
}

/// Returns true if the given string MAY include URLs or alike.
/// Returns `true` if the given string MAY include URLs or alike.
fn has_url(s: &str) -> bool {
// This function may return false positive, but should get its job done in most cases.
s.contains("https://") || s.contains("http://") || s.contains("ftp://") || s.contains("file://")
Expand Down Expand Up @@ -1000,8 990,8 @@ impl FindUncommented for str {

// Returns the first byte position after the first comment. The given string
// is expected to be prefixed by a comment, including delimiters.
// Good: "/* /* inner */ outer */ code();"
// Bad: "code(); // hello\n world!"
// Good: `/* /* inner */ outer */ code();`
// Bad: `code(); // hello\n world!`
pub fn find_comment_end(s: &str) -> Option<usize> {
let mut iter = CharClasses::new(s.char_indices());
for (kind, (i, _c)) in &mut iter {
Expand All @@ -1010,15 1000,15 @@ pub fn find_comment_end(s: &str) -> Option<usize> {
}
}

// Handle case where the comment ends at the end of s.
// Handle case where the comment ends at the end of `s`.
if iter.status == CharClassesStatus::Normal {
Some(s.len())
} else {
None
}
}

/// Returns true if text contains any comment.
/// Returns `true` if text contains any comment.
pub fn contains_comment(text: &str) -> bool {
CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
}
Expand Down Expand Up @@ -1540,7 1530,7 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
}

/// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
/// (if it fits in the width/offset, else return None), else return `new`
/// (if it fits in the width/offset, else return `None`), else return `new`
pub fn recover_comment_removed(
new: String,
span: Span,
Expand Down Expand Up @@ -1583,14 1573,14 @@ pub fn filter_normal_code(code: &str) -> String {
buffer
}

/// Return true if the two strings of code have the same payload of comments.
/// Returns `true` if the two strings of code have the same payload of comments.
/// The payload of comments is everything in the string except:
/// - actual code (not comments)
/// - comment start/end marks
/// - whitespace
/// - '*' at the beginning of lines in block comments
/// - actual code (not comments),
/// - comment start/end marks,
/// - whitespace,
/// - '*' at the beginning of lines in block comments.
fn changed_comment_content(orig: &str, new: &str) -> bool {
// Cannot write this as a fn since we cannot return types containing closures
// Cannot write this as a fn since we cannot return types containing closures.
let code_comment_content = |code| {
let slices = UngroupedCommentCodeSlices::new(code);
slices
Expand Down Expand Up @@ -1625,7 1615,8 @@ impl<'a> CommentReducer<'a> {
let comment = remove_comment_header(comment);
CommentReducer {
is_block,
at_start_line: false, // There are no supplementary '*' on the first line
// There are no supplementary '*' on the first line.
at_start_line: false,
iter: comment.chars(),
}
}
Expand All @@ -1641,7 1632,7 @@ impl<'a> Iterator for CommentReducer<'a> {
while c.is_whitespace() {
c = self.iter.next()?;
}
// Ignore leading '*'
// Ignore leading '*'.
if c == '*' {
c = self.iter.next()?;
}
Expand Down Expand Up @@ -1777,7 1768,7 @@ mod test {
&wrap_normalize_config).unwrap();
assert_eq!("/* trimmed */", comment);

// check that different comment style are properly recognised
// Check that different comment style are properly recognised.
let comment = rewrite_comment(r#"/// test1
/// test2
/*
Expand All @@ -1788,7 1779,7 @@ mod test {
&wrap_normalize_config).unwrap();
assert_eq!("/// test1\n/// test2\n// test3", comment);

// check that the blank line marks the end of a commented paragraph
// Check that the blank line marks the end of a commented paragraph.
let comment = rewrite_comment(r#"// test1
// test2"#,
Expand All @@ -1797,7 1788,7 @@ mod test {
&wrap_normalize_config).unwrap();
assert_eq!("// test1\n\n// test2", comment);

// check that the blank line marks the end of a custom-commented paragraph
// Check that the blank line marks the end of a custom-commented paragraph.
let comment = rewrite_comment(r#"//@ test1
//@ test2"#,
Expand All @@ -1806,7 1797,7 @@ mod test {
&wrap_normalize_config).unwrap();
assert_eq!("//@ test1\n\n//@ test2", comment);

// check that bare lines are just indented but left unchanged otherwise
// Check that bare lines are just indented but otherwise left unchanged.
let comment = rewrite_comment(r#"// test1
/*
a bare line!
Expand All @@ -1819,8 1810,8 @@ mod test {
assert_eq!("// test1\n/*\n a bare line!\n\n another bare line!\n*/", comment);
}

// This is probably intended to be a non-test fn, but it is not used. I'm
// keeping it around unless it helps us test stuff.
// This is probably intended to be a non-test fn, but it is not used.
// We should keep this around unless it helps us test stuff to remove it.
fn uncommented(text: &str) -> String {
CharClasses::new(text.chars())
.filter_map(|(s, c)| match s {
Expand Down
Loading

0 comments on commit 7a3b7c9

Please sign in to comment.