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 @@ -260,7 +260,7 @@ impl Rewrite for ast::MetaItemInner {
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
15 changes: 7 additions & 8 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,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 @@ -818,7 +817,7 @@ mod test {
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
assert!(config.overflow_delimited_expr());
}

#[nightly_only_test]
Expand All @@ -828,7 +827,7 @@ mod test {
let config_file = Some(Path::new("tests/config/style-edition/just-version"));
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
assert!(config.overflow_delimited_expr());
}

#[nightly_only_test]
Expand Down Expand Up @@ -873,7 +872,7 @@ mod test {
]);
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
assert!(config.overflow_delimited_expr());
}

#[nightly_only_test]
Expand Down Expand Up @@ -939,7 +938,7 @@ mod test {
options.style_edition = Some(StyleEdition::Edition2024);
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
assert!(config.overflow_delimited_expr());
}

#[nightly_only_test]
Expand All @@ -949,7 +948,7 @@ mod test {
let config_file = Some(Path::new("tests/config/style-edition/overrides"));
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), false);
assert!(!config.overflow_delimited_expr());
}

#[nightly_only_test]
Expand All @@ -961,6 +960,6 @@ mod test {
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);
assert!(!config.overflow_delimited_expr());
}
}
10 changes: 5 additions & 5 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl ChainItemKind {
fn is_tup_field_access(expr: &ast::Expr) -> bool {
match expr.kind {
ast::ExprKind::Field(_, ref field) => {
field.name.as_str().chars().all(|c| c.is_digit(10))
field.name.as_str().chars().all(|c| c.is_ascii_digit())
}
_ => false,
}
Expand Down Expand Up @@ -288,7 +288,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 @@ -353,7 +353,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 Expand Up @@ -843,7 +843,7 @@ impl<'a> ChainFormatterBlock<'a> {
}
}

impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
impl ChainFormatter for ChainFormatterBlock<'_> {
fn format_root(
&mut self,
parent: &ChainItem,
Expand Down Expand Up @@ -931,7 +931,7 @@ impl<'a> ChainFormatterVisual<'a> {
}
}

impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
impl ChainFormatter for ChainFormatterVisual<'_> {
fn format_root(
&mut self,
parent: &ChainItem,
Expand Down
12 changes: 6 additions & 6 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ fn needs_block(
prefix: &str,
context: &RewriteContext<'_>,
) -> bool {
let has_attributes = block.stmts.first().map_or(false, |first_stmt| {
!get_attrs_from_stmt(first_stmt).is_empty()
});
let has_attributes = block
.stmts
.first()
.is_some_and(|first_stmt| !get_attrs_from_stmt(first_stmt).is_empty());

is_unsafe_block(block)
|| block.stmts.len() > 1
Expand Down Expand Up @@ -434,9 +435,8 @@ pub(crate) fn rewrite_last_closure(

// When overflowing the closure which consists of a single control flow expression,
// force to use block if its condition uses multi line.
let is_multi_lined_cond = rewrite_cond(context, body, body_shape).map_or(false, |cond| {
cond.contains('\n') || cond.len() > body_shape.width
});
let is_multi_lined_cond = rewrite_cond(context, body, body_shape)
.is_some_and(|cond| cond.contains('\n') || cond.len() > body_shape.width);
if is_multi_lined_cond {
return rewrite_closure_with_block(body, &prefix, context, body_shape);
}
Expand Down
16 changes: 8 additions & 8 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,11 +990,11 @@ fn is_table_item(mut s: &str) -> bool {
// This function may return false positive, but should get its job done in most cases (i.e.
// markdown tables with two column delimiters).
s = s.trim_start();
return s.starts_with('|')
s.starts_with('|')
&& match s.rfind('|') {
Some(0) | None => false,
_ => true,
};
}
}

/// Given the span, rewrite the missing comment inside it if available.
Expand Down Expand Up @@ -1521,7 +1521,7 @@ impl<'a> LineClasses<'a> {
}
}

impl<'a> Iterator for LineClasses<'a> {
impl Iterator for LineClasses<'_> {
type Item = (FullCodeCharKind, String);

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -1797,7 +1797,7 @@ impl<'a> CommentReducer<'a> {
}
}

impl<'a> Iterator for CommentReducer<'a> {
impl Iterator for CommentReducer<'_> {
type Item = char;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -2007,10 +2007,10 @@ mod test {

#[test]
fn test_contains_comment() {
assert_eq!(contains_comment("abc"), false);
assert_eq!(contains_comment("abc // qsdf"), true);
assert_eq!(contains_comment("abc /* kqsdf"), true);
assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
assert!(!contains_comment("abc"));
assert!(contains_comment("abc // qsdf"));
assert!(contains_comment("abc /* kqsdf"));
assert!(!contains_comment("abc \" /* */\" qsdf"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/config/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,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
57 changes: 25 additions & 32 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,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(&toml, file_path, edition, style_edition, version)
Expand Down Expand Up @@ -715,8 +715,8 @@ mod test {
fn test_was_set() {
let config = Config::from_toml("hard_tabs = true", Path::new("./rustfmt.toml")).unwrap();

assert_eq!(config.was_set().hard_tabs(), true);
assert_eq!(config.was_set().verbose(), false);
assert!(config.was_set().hard_tabs());
assert!(!config.was_set().verbose());
}

const PRINT_DOCS_STABLE_OPTION: &str = "stable_option <boolean> Default: false";
Expand All @@ -732,9 +732,9 @@ mod test {
Config::print_docs(&mut output, false);

let s = str::from_utf8(&output).unwrap();
assert_eq!(s.contains(PRINT_DOCS_STABLE_OPTION), true);
assert_eq!(s.contains(PRINT_DOCS_UNSTABLE_OPTION), false);
assert_eq!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION), true);
assert!(s.contains(PRINT_DOCS_STABLE_OPTION));
assert!(!s.contains(PRINT_DOCS_UNSTABLE_OPTION));
assert!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION));
}

#[test]
Expand All @@ -745,9 +745,9 @@ mod test {
Config::print_docs(&mut output, true);

let s = str::from_utf8(&output).unwrap();
assert_eq!(s.contains(PRINT_DOCS_STABLE_OPTION), true);
assert_eq!(s.contains(PRINT_DOCS_UNSTABLE_OPTION), true);
assert_eq!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION), true);
assert!(s.contains(PRINT_DOCS_STABLE_OPTION));
assert!(s.contains(PRINT_DOCS_UNSTABLE_OPTION));
assert!(s.contains(PRINT_DOCS_PARTIALLY_UNSTABLE_OPTION));
}

#[test]
Expand Down Expand Up @@ -966,32 +966,32 @@ make_backup = false
config.set().unstable_features(true);
// When we don't set the config from toml or command line options it
// doesn't get marked as set by the user.
assert_eq!(config.was_set().unstable_features(), false);
assert!(!config.was_set().unstable_features());
config.set().unstable_features(true);
assert_eq!(config.unstable_features(), true);
assert!(config.unstable_features());
}

#[nightly_only_test]
#[test]
fn test_unstable_from_toml() {
let config =
Config::from_toml("unstable_features = true", Path::new("./rustfmt.toml")).unwrap();
assert_eq!(config.was_set().unstable_features(), true);
assert_eq!(config.unstable_features(), true);
assert!(config.was_set().unstable_features());
assert!(config.unstable_features());
}

#[test]
fn test_set_cli() {
let mut config = Config::default();
assert_eq!(config.was_set().edition(), false);
assert_eq!(config.was_set_cli().edition(), false);
assert!(!config.was_set().edition());
assert!(!config.was_set_cli().edition());
config.set().edition(Edition::Edition2021);
assert_eq!(config.was_set().edition(), false);
assert_eq!(config.was_set_cli().edition(), false);
assert!(!config.was_set().edition());
assert!(!config.was_set_cli().edition());
config.set_cli().edition(Edition::Edition2021);
assert_eq!(config.was_set().edition(), false);
assert_eq!(config.was_set_cli().edition(), true);
assert_eq!(config.was_set_cli().emit_mode(), false);
assert!(!config.was_set().edition());
assert!(config.was_set_cli().edition());
assert!(!config.was_set_cli().emit_mode());
}

#[cfg(test)]
Expand Down Expand Up @@ -1365,7 +1365,7 @@ make_backup = false
let versions = vec!["0.0.0", "0.0.1", "0.1.0"];

for version in versions {
let toml = format!("required_version=\"{}\"", version.to_string());
let toml = format!("required_version=\"{}\"", version);
let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();

assert!(!config.version_meets_requirement());
Expand All @@ -1389,8 +1389,7 @@ make_backup = false
for minor in current_version.minor..0 {
let toml = format!(
"required_version=\"^{}.{}.0\"",
current_version.major.to_string(),
minor.to_string()
current_version.major, minor
);
let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();

Expand Down Expand Up @@ -1434,7 +1433,7 @@ make_backup = false
#[nightly_only_test]
#[test]
fn test_required_version_exact_boundary() {
let toml = format!("required_version=\"{}\"", get_current_version().to_string());
let toml = format!("required_version=\"{}\"", get_current_version());
let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();

assert!(config.version_meets_requirement());
Expand All @@ -1443,10 +1442,7 @@ make_backup = false
#[nightly_only_test]
#[test]
fn test_required_version_pre_release() {
let toml = format!(
"required_version=\"^{}-alpha\"",
get_current_version().to_string()
);
let toml = format!("required_version=\"^{}-alpha\"", get_current_version());
let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();

assert!(config.version_meets_requirement());
Expand All @@ -1455,10 +1451,7 @@ make_backup = false
#[nightly_only_test]
#[test]
fn test_required_version_with_build_metadata() {
let toml = format!(
"required_version=\"{}+build.1\"",
get_current_version().to_string()
);
let toml = format!("required_version=\"{}+build.1\"", get_current_version());

let config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap();

Expand Down
16 changes: 4 additions & 12 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ pub enum ReportTactic {

/// What Rustfmt should emit. Mostly corresponds to the `--emit` command line
/// option.
#[derive(Default)]
#[config_type]
pub enum EmitMode {
/// Emits to files.
#[default]
Files,
/// Writes the output to stdout.
Stdout,
Expand Down Expand Up @@ -325,12 +327,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 @@ -440,11 +436,13 @@ pub trait CliOptions {
}

/// The edition of the syntax and semantics of code (RFC 2052).
#[derive(Default)]
#[config_type]
pub enum Edition {
#[value = "2015"]
#[doc_hint = "2015"]
/// Edition 2015.
#[default]
Edition2015,
#[value = "2018"]
#[doc_hint = "2018"]
Expand All @@ -460,12 +458,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
Loading
Loading