Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl Rewrite for ast::NestedMetaItem {
fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
// Look at before and after comment and see if there are any empty lines.
let comment_begin = comment.find('/');
let len = comment_begin.unwrap_or_else(|| comment.len());
let len = comment_begin.unwrap_or(comment.len());
let mlb = count_newlines(&comment[..len]) > 1;
let mla = if comment_begin.is_none() {
mlb
Expand Down
122 changes: 74 additions & 48 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,15 @@ fn print_version() {

fn determine_operation(matches: &Matches) -> Result<Operation, OperationError> {
if matches.opt_present("h") {
let topic = matches.opt_str("h");
if topic.is_none() {
let Some(topic) = matches.opt_str("h") else {
return Ok(Operation::Help(HelpOp::None));
} else if topic == Some("config".to_owned()) {
return Ok(Operation::Help(HelpOp::Config));
} else if topic == Some("file-lines".to_owned()) && is_nightly() {
return Ok(Operation::Help(HelpOp::FileLines));
} else {
return Err(OperationError::UnknownHelpTopic(topic.unwrap()));
}
};

return match topic.as_str() {
"config" => Ok(Operation::Help(HelpOp::Config)),
"file-lines" if is_nightly() => Ok(Operation::Help(HelpOp::FileLines)),
_ => Err(OperationError::UnknownHelpTopic(topic)),
};
}
let mut free_matches = matches.free.iter();

Expand Down Expand Up @@ -547,9 +546,11 @@ struct GetOptsOptions {

impl GetOptsOptions {
pub fn from_matches(matches: &Matches) -> Result<GetOptsOptions> {
let mut options = GetOptsOptions::default();
options.verbose = matches.opt_present("verbose");
options.quiet = matches.opt_present("quiet");
let mut options = GetOptsOptions {
verbose: matches.opt_present("verbose"),
quiet: matches.opt_present("quiet"),
..Default::default()
};
if options.verbose && options.quiet {
return Err(format_err!("Can't use both `--verbose` and `--quiet`"));
}
Expand Down Expand Up @@ -750,8 +751,7 @@ impl CliOptions for GetOptsOptions {
fn version(&self) -> Option<Version> {
self.inline_config
.get("version")
.map(|version| Version::from_str(version).ok())
.flatten()
.and_then(|version| Version::from_str(version).ok())
}
}

Expand Down Expand Up @@ -799,26 +799,32 @@ mod test {
#[nightly_only_test]
#[test]
fn flag_sets_style_edition_override_correctly() {
let mut options = GetOptsOptions::default();
options.style_edition = Some(StyleEdition::Edition2024);
let options = GetOptsOptions {
style_edition: Some(StyleEdition::Edition2024),
..Default::default()
};
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
}

#[nightly_only_test]
#[test]
fn edition_sets_style_edition_override_correctly() {
let mut options = GetOptsOptions::default();
options.edition = Some(Edition::Edition2024);
let options = GetOptsOptions {
edition: Some(Edition::Edition2024),
..Default::default()
};
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
}

#[nightly_only_test]
#[test]
fn version_sets_style_edition_override_correctly() {
let mut options = GetOptsOptions::default();
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
let options = GetOptsOptions {
inline_config: HashMap::from([("version".to_owned(), "Two".to_owned())]),
..Default::default()
};
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
Expand All @@ -837,43 +843,51 @@ mod test {
#[nightly_only_test]
#[test]
fn style_edition_flag_has_correct_precedence_over_edition() {
let mut options = GetOptsOptions::default();
options.style_edition = Some(StyleEdition::Edition2021);
options.edition = Some(Edition::Edition2024);
let options = GetOptsOptions {
style_edition: Some(StyleEdition::Edition2021),
edition: Some(Edition::Edition2024),
..Default::default()
};
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
}

#[nightly_only_test]
#[test]
fn style_edition_flag_has_correct_precedence_over_version() {
let mut options = GetOptsOptions::default();
options.style_edition = Some(StyleEdition::Edition2018);
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
let options = GetOptsOptions {
style_edition: Some(StyleEdition::Edition2018),
inline_config: HashMap::from([("version".to_owned(), "Two".to_owned())]),
..Default::default()
};
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2018);
}

#[nightly_only_test]
#[test]
fn style_edition_flag_has_correct_precedence_over_edition_version() {
let mut options = GetOptsOptions::default();
options.style_edition = Some(StyleEdition::Edition2021);
options.edition = Some(Edition::Edition2018);
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
let options = GetOptsOptions {
style_edition: Some(StyleEdition::Edition2021),
edition: Some(Edition::Edition2018),
inline_config: HashMap::from([("version".to_owned(), "Two".to_owned())]),
..Default::default()
};
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
}

#[nightly_only_test]
#[test]
fn style_edition_inline_has_correct_precedence_over_edition_version() {
let mut options = GetOptsOptions::default();
options.edition = Some(Edition::Edition2018);
options.inline_config = HashMap::from([
("version".to_owned(), "One".to_owned()),
("style_edition".to_owned(), "2024".to_owned()),
]);
let options = GetOptsOptions {
edition: Some(Edition::Edition2018),
inline_config: HashMap::from([
("version".to_owned(), "One".to_owned()),
("style_edition".to_owned(), "2024".to_owned()),
]),
..Default::default()
};
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
Expand All @@ -882,22 +896,26 @@ mod test {
#[nightly_only_test]
#[test]
fn style_edition_config_file_trumps_edition_flag_version_inline() {
let mut options = GetOptsOptions::default();
let options = GetOptsOptions {
edition: Some(Edition::Edition2018),
inline_config: HashMap::from([("version".to_owned(), "One".to_owned())]),
..Default::default()
};
let config_file = Some(Path::new("tests/config/style-edition/just-style-edition"));
options.edition = Some(Edition::Edition2018);
options.inline_config = HashMap::from([("version".to_owned(), "One".to_owned())]);
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
}

#[nightly_only_test]
#[test]
fn style_edition_config_file_trumps_edition_config_and_version_inline() {
let mut options = GetOptsOptions::default();
let options = GetOptsOptions {
inline_config: HashMap::from([("version".to_owned(), "Two".to_owned())]),
..Default::default()
};
let config_file = Some(Path::new(
"tests/config/style-edition/style-edition-and-edition",
));
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
assert_eq!(config.edition(), Edition::Edition2024);
Expand All @@ -906,9 +924,11 @@ mod test {
#[nightly_only_test]
#[test]
fn version_config_trumps_edition_config_and_flag() {
let mut options = GetOptsOptions::default();
let options = GetOptsOptions {
edition: Some(Edition::Edition2018),
..Default::default()
};
let config_file = Some(Path::new("tests/config/style-edition/version-edition"));
options.edition = Some(Edition::Edition2018);
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
}
Expand Down Expand Up @@ -938,8 +958,10 @@ mod test {
#[nightly_only_test]
#[test]
fn correct_defaults_for_style_edition_loaded() {
let mut options = GetOptsOptions::default();
options.style_edition = Some(StyleEdition::Edition2024);
let options = GetOptsOptions {
style_edition: Some(StyleEdition::Edition2024),
..Default::default()
};
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
Expand All @@ -958,10 +980,14 @@ mod test {
#[nightly_only_test]
#[test]
fn style_edition_defaults_overridden_from_cli() {
let mut options = GetOptsOptions::default();
let options = GetOptsOptions {
inline_config: HashMap::from([(
"overflow_delimited_expr".to_owned(),
"false".to_owned(),
)]),
..Default::default()
};
let config_file = Some(Path::new("tests/config/style-edition/just-style-edition"));
options.inline_config =
HashMap::from([("overflow_delimited_expr".to_owned(), "false".to_owned())]);
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), false);
Expand Down
6 changes: 3 additions & 3 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl ChainItemKind {
fn is_tup_field_access(expr: &ast::Expr) -> bool {
match expr.kind {
ast::ExprKind::Field(_, ref field) => {
field.name.to_string().chars().all(|c| c.is_digit(10))
field.name.to_string().chars().all(|c| c.is_ascii_digit())
}
_ => false,
}
Expand Down Expand Up @@ -286,7 +286,7 @@ impl Rewrite for ChainItem {
ChainItemKind::Parent {
ref expr,
parens: true,
} => crate::expr::rewrite_paren(context, &expr, shape, expr.span)?,
} => crate::expr::rewrite_paren(context, expr, shape, expr.span)?,
ChainItemKind::Parent {
ref expr,
parens: false,
Expand Down Expand Up @@ -351,7 +351,7 @@ impl ChainItem {
format!("::<{}>", type_list.join(", "))
};
let callee_str = format!(".{}{}", rewrite_ident(context, method_name), type_str);
rewrite_call(context, &callee_str, &args, span, shape)
rewrite_call(context, &callee_str, args, span, shape)
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,11 @@ impl ItemizedBlock {

/// Returns the block as a string, with each line trimmed at the start.
fn trimmed_block_as_string(&self) -> String {
self.lines
.iter()
.map(|line| format!("{} ", line.trim_start()))
.collect::<String>()
self.lines.iter().fold(String::new(), |mut acc, line| {
acc.push_str(line.trim_start());
acc.push(' ');
acc
})
}

/// Returns the block as a string under its original form.
Expand Down
12 changes: 6 additions & 6 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Config {
style_edition: Option<StyleEdition>,
version: Option<Version>,
) -> Result<Config, Error> {
let mut file = File::open(&file_path)?;
let mut file = File::open(file_path)?;
let mut toml = String::new();
file.read_to_string(&mut toml)?;
Config::from_toml_for_style_edition(
Expand Down Expand Up @@ -900,7 +900,7 @@ make_backup = false
Config::default_with_style_edition(style_edition)
.all_options()
.to_toml()
.unwrap();
.unwrap()
};
let edition2015 = get_edition_toml(StyleEdition::Edition2015);
let edition2018 = get_edition_toml(StyleEdition::Edition2018);
Expand Down Expand Up @@ -1048,10 +1048,10 @@ make_backup = false
max_width = 100
"#;
let config = Config::from_toml(toml, Path::new("")).unwrap();
assert_eq!(config.array_width(), usize::max_value());
assert_eq!(config.attr_fn_like_width(), usize::max_value());
assert_eq!(config.chain_width(), usize::max_value());
assert_eq!(config.fn_call_width(), usize::max_value());
assert_eq!(config.array_width(), usize::MAX);
assert_eq!(config.attr_fn_like_width(), usize::MAX);
assert_eq!(config.chain_width(), usize::MAX);
assert_eq!(config.fn_call_width(), usize::MAX);
assert_eq!(config.single_line_if_else_max_width(), 0);
assert_eq!(config.struct_lit_width(), 0);
assert_eq!(config.struct_variant_width(), 0);
Expand Down
24 changes: 8 additions & 16 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ pub enum ReportTactic {

/// What Rustfmt should emit. Mostly corresponds to the `--emit` command line
/// option.
#[derive(Default)]
#[config_type]
pub enum EmitMode {
#[default]
/// Emits to files.
Files,
/// Writes the output to stdout.
Expand Down Expand Up @@ -255,12 +257,12 @@ impl WidthHeuristics {
// Using this WidthHeuristics means we ignore heuristics.
pub fn null() -> WidthHeuristics {
WidthHeuristics {
fn_call_width: usize::max_value(),
attr_fn_like_width: usize::max_value(),
fn_call_width: usize::MAX,
attr_fn_like_width: usize::MAX,
struct_lit_width: 0,
struct_variant_width: 0,
array_width: usize::max_value(),
chain_width: usize::max_value(),
array_width: usize::MAX,
chain_width: usize::MAX,
single_line_if_else_max_width: 0,
single_line_let_else_max_width: 0,
}
Expand Down Expand Up @@ -310,12 +312,6 @@ impl ::std::str::FromStr for WidthHeuristics {
}
}

impl Default for EmitMode {
fn default() -> EmitMode {
EmitMode::Files
}
}

/// A set of directories, files and modules that rustfmt should ignore.
#[derive(Default, Clone, Debug, PartialEq)]
pub struct IgnoreList {
Expand Down Expand Up @@ -425,8 +421,10 @@ pub trait CliOptions {
}

/// The edition of the syntax and semantics of code (RFC 2052).
#[derive(Default)]
#[config_type]
pub enum Edition {
#[default]
#[value = "2015"]
#[doc_hint = "2015"]
/// Edition 2015.
Expand All @@ -445,12 +443,6 @@ pub enum Edition {
Edition2024,
}

impl Default for Edition {
fn default() -> Edition {
Edition::Edition2015
}
}

impl From<Edition> for rustc_span::edition::Edition {
fn from(edition: Edition) -> Self {
match edition {
Expand Down
2 changes: 1 addition & 1 deletion src/emitter/checkstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ mod tests {
let lib_original = ["fn greet() {", "println!(\"Greetings!\");", "}"];
let lib_formatted = ["fn greet() {", " println!(\"Greetings!\");", "}"];
let mut writer = Vec::new();
let mut emitter = CheckstyleEmitter::default();
let mut emitter = CheckstyleEmitter;
let _ = emitter.emit_header(&mut writer);
let _ = emitter
.emit_formatted_file(
Expand Down
Loading
Loading