Skip to content

Commit 82913d6

Browse files
authored
feat: upgrade to rust edition 2024 (#228)
Use of `unsafe` blocks (in tests only) is ok due to use of cargo-nextest, which creates a separate env for each test (run in parallel). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Simplified control flow logic by consolidating nested conditions into more concise patterns throughout the codebase. * Reorganized imports for improved code consistency and readability. * **Chores** * Updated Rust edition configuration to use workspace-managed versioning across all packages. * **Tests** * Enhanced test infrastructure by adjusting environment variable handling in test setup procedures.
1 parent 6523512 commit 82913d6

File tree

20 files changed

+343
-279
lines changed

20 files changed

+343
-279
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ authors = [
1313
description = "Run clang-format and clang-tidy on a batch of files."
1414
homepage = "https://cpp-linter.github.io/cpp-linter-rs"
1515
license = "MIT"
16+
edition = "2024"
1617

1718
[profile.release]
1819
lto = true

bindings/node/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
22
name = "cpp-linter-js"
3-
edition = "2021"
43
readme = "README.md"
54
keywords = ["clang-tidy", "clang-format", "linter"]
65
categories = ["command-line-utilities", "development-tools", "filesystem"]
76
repository = "https://github.com/cpp-linter/cpp-linter-rs"
87
version.workspace = true
8+
edition.workspace = true
99
authors.workspace = true
1010
description.workspace = true
1111
homepage.workspace = true

bindings/python/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "cpp-linter-py"
3-
edition = "2021"
43
readme = "README.md"
54
repository = "https://github.com/cpp-linter/cpp-linter-rs/tree/main/bindings/python"
65
version.workspace = true
6+
edition.workspace = true
77
authors.workspace = true
88
description.workspace = true
99
homepage.workspace = true

cpp-linter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
22
name = "cpp-linter"
3-
edition = "2021"
43
readme = "README.md"
54
keywords = ["clang-tidy", "clang-format", "linter"]
65
categories = ["command-line-utilities", "development-tools", "filesystem"]
76
repository = "https://github.com/cpp-linter/cpp-linter-rs"
87
version.workspace = true
8+
edition.workspace = true
99
authors.workspace = true
1010
description.workspace = true
1111
homepage.workspace = true

cpp-linter/src/clang_tools/clang_format.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use serde::Deserialize;
1515
use super::MakeSuggestions;
1616
use crate::{
1717
cli::ClangParams,
18-
common_fs::{get_line_count_from_offset, FileObj},
18+
common_fs::{FileObj, get_line_count_from_offset},
1919
};
2020

2121
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
@@ -71,10 +71,10 @@ pub fn tally_format_advice(files: &[Arc<Mutex<FileObj>>]) -> u64 {
7171
let mut total = 0;
7272
for file in files {
7373
let file = file.lock().unwrap();
74-
if let Some(advice) = &file.format_advice {
75-
if !advice.replacements.is_empty() {
76-
total += 1;
77-
}
74+
if let Some(advice) = &file.format_advice
75+
&& !advice.replacements.is_empty()
76+
{
77+
total += 1;
7878
}
7979
}
8080
total
@@ -187,7 +187,7 @@ pub fn run_clang_format(
187187

188188
#[cfg(test)]
189189
mod tests {
190-
use super::{summarize_style, FormatAdvice, Replacement};
190+
use super::{FormatAdvice, Replacement, summarize_style};
191191

192192
#[test]
193193
fn parse_blank_xml() {

cpp-linter/src/clang_tools/clang_tidy.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use serde::Deserialize;
1818
use super::MakeSuggestions;
1919
use crate::{
2020
cli::{ClangParams, LinesChangedOnly},
21-
common_fs::{normalize_path, FileObj},
21+
common_fs::{FileObj, normalize_path},
2222
};
2323

2424
/// Used to deserialize a json compilation database's translation unit.
@@ -205,21 +205,19 @@ fn parse_tidy_output(
205205
found_fix = false;
206206
} else if let Some(capture) = fixed_note.captures(line) {
207207
let fixed_line = capture[1].parse()?;
208-
if let Some(note) = &mut notification {
209-
if !note.fixed_lines.contains(&fixed_line) {
210-
note.fixed_lines.push(fixed_line);
211-
}
208+
if let Some(note) = &mut notification
209+
&& !note.fixed_lines.contains(&fixed_line)
210+
{
211+
note.fixed_lines.push(fixed_line);
212212
}
213213
// Suspend capturing subsequent lines as suggestions until
214214
// a new notification is constructed. If we found a note about applied fixes,
215215
// then the lines of suggestions for that notification have already been parsed.
216216
found_fix = true;
217-
} else if !found_fix {
218-
if let Some(note) = &mut notification {
219-
// append lines of code that are part of
220-
// the previous line's notification
221-
note.suggestion.push(line.to_string());
222-
}
217+
} else if !found_fix && let Some(note) = &mut notification {
218+
// append lines of code that are part of
219+
// the previous line's notification
220+
note.suggestion.push(line.to_string());
223221
}
224222
}
225223
if let Some(note) = notification {
@@ -358,7 +356,7 @@ mod test {
358356
common_fs::FileObj,
359357
};
360358

361-
use super::{run_clang_tidy, TidyNotification, NOTE_HEADER};
359+
use super::{NOTE_HEADER, TidyNotification, run_clang_tidy};
362360

363361
#[test]
364362
fn clang_diagnostic_link() {

cpp-linter/src/clang_tools/mod.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{
1111
};
1212

1313
// non-std crates
14-
use anyhow::{anyhow, Context, Result};
14+
use anyhow::{Context, Result, anyhow};
1515
use git2::{DiffOptions, Patch};
1616
use regex::Regex;
1717
use semver::Version;
@@ -22,12 +22,12 @@ use which::{which, which_in};
2222
use super::common_fs::FileObj;
2323
use crate::{
2424
cli::{ClangParams, RequestedVersion},
25-
rest_api::{RestApiClient, COMMENT_MARKER, USER_OUTREACH},
25+
rest_api::{COMMENT_MARKER, RestApiClient, USER_OUTREACH},
2626
};
2727
pub mod clang_format;
2828
use clang_format::run_clang_format;
2929
pub mod clang_tidy;
30-
use clang_tidy::{run_clang_tidy, CompilationUnit};
30+
use clang_tidy::{CompilationUnit, run_clang_tidy};
3131

3232
#[derive(Debug)]
3333
pub enum ClangTool {
@@ -229,14 +229,14 @@ pub async fn capture_clang_tools_output(
229229
}
230230

231231
// parse database (if provided) to match filenames when parsing clang-tidy's stdout
232-
if let Some(db_path) = &clang_params.database {
233-
if let Ok(db_str) = fs::read(db_path.join("compile_commands.json")) {
234-
clang_params.database_json = Some(
235-
// A compilation database should be UTF-8 encoded, but file paths are not; use lossy conversion.
236-
serde_json::from_str::<Vec<CompilationUnit>>(&String::from_utf8_lossy(&db_str))
237-
.with_context(|| "Failed to parse compile_commands.json")?,
238-
)
239-
}
232+
if let Some(db_path) = &clang_params.database
233+
&& let Ok(db_str) = fs::read(db_path.join("compile_commands.json"))
234+
{
235+
clang_params.database_json = Some(
236+
// A compilation database should be UTF-8 encoded, but file paths are not; use lossy conversion.
237+
serde_json::from_str::<Vec<CompilationUnit>>(&String::from_utf8_lossy(&db_str))
238+
.with_context(|| "Failed to parse compile_commands.json")?,
239+
)
240240
};
241241

242242
let mut executors = JoinSet::new();
@@ -503,24 +503,26 @@ mod tests {
503503
let req_version = RequestedVersion::from_str(requirement).unwrap();
504504
let tool_exe = CLANG_FORMAT.get_exe_path(&req_version);
505505
println!("tool_exe: {:?}", tool_exe);
506-
assert!(tool_exe.is_ok_and(|val| val
507-
.file_name()
508-
.unwrap()
509-
.to_string_lossy()
510-
.to_string()
511-
.contains(CLANG_FORMAT.as_str())));
506+
assert!(tool_exe.is_ok_and(|val| {
507+
val.file_name()
508+
.unwrap()
509+
.to_string_lossy()
510+
.to_string()
511+
.contains(CLANG_FORMAT.as_str())
512+
}));
512513
}
513514

514515
#[test]
515516
fn get_exe_by_default() {
516517
let tool_exe = CLANG_FORMAT.get_exe_path(&RequestedVersion::from_str("").unwrap());
517518
println!("tool_exe: {:?}", tool_exe);
518-
assert!(tool_exe.is_ok_and(|val| val
519-
.file_name()
520-
.unwrap()
521-
.to_string_lossy()
522-
.to_string()
523-
.contains(CLANG_FORMAT.as_str())));
519+
assert!(tool_exe.is_ok_and(|val| {
520+
val.file_name()
521+
.unwrap()
522+
.to_string_lossy()
523+
.to_string()
524+
.contains(CLANG_FORMAT.as_str())
525+
}));
524526
}
525527

526528
#[test]
@@ -531,12 +533,13 @@ mod tests {
531533
println!("binary exe path: {bin_path}");
532534
let tool_exe = CLANG_FORMAT.get_exe_path(&RequestedVersion::from_str(bin_path).unwrap());
533535
println!("tool_exe: {:?}", tool_exe);
534-
assert!(tool_exe.is_ok_and(|val| val
535-
.file_name()
536-
.unwrap()
537-
.to_string_lossy()
538-
.to_string()
539-
.contains(TOOL_NAME)));
536+
assert!(tool_exe.is_ok_and(|val| {
537+
val.file_name()
538+
.unwrap()
539+
.to_string_lossy()
540+
.to_string()
541+
.contains(TOOL_NAME)
542+
}));
540543
}
541544

542545
#[test]

cpp-linter/src/cli/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::{path::PathBuf, str::FromStr};
33

44
// non-std crates
55
use clap::{
6+
ArgAction, Args, Parser, Subcommand, ValueEnum,
67
builder::{FalseyValueParser, NonEmptyStringValueParser},
7-
value_parser, ArgAction, Args, Parser, Subcommand, ValueEnum,
8+
value_parser,
89
};
910

1011
mod structs;
@@ -453,7 +454,7 @@ pub fn convert_extra_arg_val(args: &[String]) -> Vec<String> {
453454

454455
#[cfg(test)]
455456
mod test {
456-
use super::{convert_extra_arg_val, Cli};
457+
use super::{Cli, convert_extra_arg_val};
457458
use clap::Parser;
458459

459460
#[test]

cpp-linter/src/cli/structs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::{fmt::Display, path::PathBuf, str::FromStr};
22

3-
use anyhow::{anyhow, Error};
4-
use clap::{builder::PossibleValue, ValueEnum};
3+
use anyhow::{Error, anyhow};
4+
use clap::{ValueEnum, builder::PossibleValue};
55
use semver::VersionReq;
66

77
use super::Cli;
88
use crate::{
99
clang_tools::clang_tidy::CompilationUnit,
10-
common_fs::{normalize_path, FileFilter},
10+
common_fs::{FileFilter, normalize_path},
1111
};
1212

1313
#[derive(Debug, Clone, PartialEq, Eq, Default)]

cpp-linter/src/common_fs/file_filter.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{anyhow, Context, Result};
1+
use anyhow::{Context, Result, anyhow};
22
use fast_glob::glob_match;
33
use std::{
44
fs,
@@ -257,14 +257,16 @@ mod tests {
257257
let files = file_filter.list_source_files(".").unwrap();
258258
assert!(!files.is_empty());
259259
for file in files {
260-
assert!(extensions.contains(
261-
&file
262-
.name
263-
.extension()
264-
.unwrap_or_default()
265-
.to_string_lossy()
266-
.to_string()
267-
));
260+
assert!(
261+
extensions.contains(
262+
&file
263+
.name
264+
.extension()
265+
.unwrap_or_default()
266+
.to_string_lossy()
267+
.to_string()
268+
)
269+
);
268270
}
269271
}
270272
}

0 commit comments

Comments
 (0)