Skip to content

Commit 0590609

Browse files
committed
Introduce a cargo clippy run
This might help catch stylistic problems in our Rust code.
1 parent 8bd2dd0 commit 0590609

File tree

15 files changed

+80
-59
lines changed

15 files changed

+80
-59
lines changed

Diff for: .github/workflows/build.yml

-30
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,6 @@ env:
1010
CARGO_TERM_COLOR: always
1111

1212
jobs:
13-
format:
14-
runs-on: ubuntu-latest
15-
steps:
16-
- name: Checkout
17-
uses: actions/checkout@v4
18-
19-
- name: Install formatting dependencies
20-
run: |
21-
sudo apt update
22-
sudo apt install gettext yapf3
23-
24-
- name: Install nightly rustfmt
25-
run: |
26-
rustup default nightly
27-
rustup component add rustfmt
28-
29-
- name: Check formatting
30-
uses: dprint/[email protected]
31-
32-
typos:
33-
runs-on: ubuntu-latest
34-
steps:
35-
- name: Checkout
36-
uses: actions/checkout@v4
37-
38-
- name: Check for typos
39-
uses: crate-ci/[email protected]
40-
with:
41-
config: ./.github/typos.toml
42-
4313
cargo:
4414
strategy:
4515
matrix:

Diff for: .github/workflows/lint.yml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Lint
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
format:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Install formatting dependencies
20+
run: |
21+
sudo apt update
22+
sudo apt install gettext yapf3
23+
24+
- name: Install nightly rustfmt
25+
run: |
26+
rustup default nightly
27+
rustup component add rustfmt
28+
29+
- name: Check formatting
30+
uses: dprint/[email protected]
31+
32+
typos:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v4
37+
38+
- name: Check for typos
39+
uses: crate-ci/[email protected]
40+
with:
41+
config: ./.github/typos.toml
42+
43+
clippy:
44+
runs-on: ubuntu-latest
45+
steps:
46+
- name: Checkout
47+
uses: actions/checkout@v4
48+
49+
- name: Setup Rust cache
50+
uses: ./.github/workflows/setup-rust-cache
51+
52+
- name: Clippy
53+
run: cargo clippy

Diff for: mdbook-course/src/bin/course-schedule.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn timediff(actual: u64, target: u64, slop: u64) -> String {
4848
} else if actual + slop < target {
4949
format!("{}: ({} short)", duration(actual), duration(target - actual),)
5050
} else {
51-
format!("{}", duration(actual))
51+
duration(actual).to_string()
5252
}
5353
}
5454

Diff for: mdbook-course/src/bin/mdbook-course.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() {
2929
);
3030
let matches = app.get_matches();
3131

32-
if let Some(_) = matches.subcommand_matches("supports") {
32+
if matches.subcommand_matches("supports").is_some() {
3333
// Support all renderers.
3434
process::exit(0);
3535
}

Diff for: mdbook-course/src/course.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Courses {
156156
fn course_mut(&mut self, name: impl AsRef<str>) -> &mut Course {
157157
let name = name.as_ref();
158158
if let Some(found_idx) =
159-
self.courses.iter().position(|course| &course.name == name)
159+
self.courses.iter().position(|course| course.name == name)
160160
{
161161
return &mut self.courses[found_idx];
162162
}
@@ -177,9 +177,7 @@ impl Courses {
177177
&self,
178178
chapter: &Chapter,
179179
) -> Option<(&Course, &Session, &Segment, &Slide)> {
180-
let Some(ref source_path) = chapter.source_path else {
181-
return None;
182-
};
180+
let source_path = chapter.source_path.as_ref()?;
183181

184182
for course in self {
185183
for session in course {
@@ -193,7 +191,7 @@ impl Courses {
193191
}
194192
}
195193

196-
return None;
194+
None
197195
}
198196
}
199197

@@ -202,7 +200,7 @@ impl<'a> IntoIterator for &'a Courses {
202200
type IntoIter = std::slice::Iter<'a, Course>;
203201

204202
fn into_iter(self) -> Self::IntoIter {
205-
(&self.courses).into_iter()
203+
self.courses.iter()
206204
}
207205
}
208206

@@ -216,7 +214,7 @@ impl Course {
216214
fn session_mut(&mut self, name: impl AsRef<str>) -> &mut Session {
217215
let name = name.as_ref();
218216
if let Some(found_idx) =
219-
self.sessions.iter().position(|session| &session.name == name)
217+
self.sessions.iter().position(|session| session.name == name)
220218
{
221219
return &mut self.sessions[found_idx];
222220
}
@@ -275,7 +273,7 @@ impl<'a> IntoIterator for &'a Course {
275273
type IntoIter = std::slice::Iter<'a, Session>;
276274

277275
fn into_iter(self) -> Self::IntoIter {
278-
(&self.sessions).into_iter()
276+
self.sessions.iter()
279277
}
280278
}
281279

@@ -350,7 +348,7 @@ impl<'a> IntoIterator for &'a Session {
350348
type IntoIter = std::slice::Iter<'a, Segment>;
351349

352350
fn into_iter(self) -> Self::IntoIter {
353-
(&self.segments).into_iter()
351+
self.segments.iter()
354352
}
355353
}
356354

@@ -403,7 +401,7 @@ impl<'a> IntoIterator for &'a Segment {
403401
type IntoIter = std::slice::Iter<'a, Slide>;
404402

405403
fn into_iter(self) -> Self::IntoIter {
406-
(&self.slides).into_iter()
404+
self.slides.iter()
407405
}
408406
}
409407

@@ -451,7 +449,7 @@ impl Slide {
451449
pub fn is_sub_chapter(&self, chapter: &Chapter) -> bool {
452450
// The first `source_path` in the slide is the "parent" chapter, so anything
453451
// else is a sub-chapter.
454-
chapter.source_path.as_ref() != self.source_paths.get(0)
452+
chapter.source_path.as_ref() != self.source_paths.first()
455453
}
456454

457455
/// Return the total duration of this slide.

Diff for: mdbook-course/src/markdown.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<const N: usize> Table<N> {
8383
for cell in iter {
8484
write!(f, " {} |", cell)?;
8585
}
86-
write!(f, "\n")
86+
writeln!(f)
8787
}
8888
}
8989

Diff for: mdbook-course/src/replacements.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn replace(
5050
}
5151
["course", "outline", course_name] => {
5252
let Some(course) = courses.find_course(course_name) else {
53-
return format!("not found - {}", captures[0].to_string());
53+
return format!("not found - {}", &captures[0]);
5454
};
5555
course.schedule()
5656
}

Diff for: mdbook-exerciser/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn process(output_directory: &Path, input_contents: &str) -> anyhow::Result<
5858
Event::Text(text) => {
5959
info!("Text: {:?}", text);
6060
if let Some(output_file) = &mut current_file {
61-
output_file.write(text.as_bytes())?;
61+
output_file.write_all(text.as_bytes())?;
6262
}
6363
}
6464
Event::End(TagEnd::CodeBlock) => {

Diff for: src/concurrency/async-exercises/chat-async/src/bin/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async fn main() -> Result<(), tokio_websockets::Error> {
4141
println!("From server: {}", text);
4242
}
4343
},
44-
Some(Err(err)) => return Err(err.into()),
44+
Some(Err(err)) => return Err(err),
4545
None => return Ok(()),
4646
}
4747
}

Diff for: src/error-handling/exercise.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ enum ParserError {
101101
fn parse(input: &str) -> Result<Expression, ParserError> {
102102
let mut tokens = tokenize(input);
103103

104-
fn parse_expr<'a>(
105-
tokens: &mut Tokenizer<'a>,
106-
) -> Result<Expression, ParserError> {
104+
fn parse_expr(tokens: &mut Tokenizer<'_>) -> Result<Expression, ParserError> {
107105
let tok = tokens.next().ok_or(ParserError::UnexpectedEOF)??;
108106
let expr = match tok {
109107
Token::Number(num) => {

Diff for: src/iterators/exercise.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ where
2525
N: Copy + std::ops::Sub<Output = N>,
2626
{
2727
// ANCHOR_END: offset_differences
28-
let a = (&values).into_iter();
29-
let b = (&values).into_iter().cycle().skip(offset);
28+
let a = values.iter();
29+
let b = values.iter().cycle().skip(offset);
3030
a.zip(b).map(|(a, b)| *b - *a).collect()
3131
}
3232

Diff for: src/tuples-and-arrays/exercise.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Iterators are covered later.
16+
#[allow(clippy::needless_range_loop)]
1517
// ANCHOR: solution
1618
// ANCHOR: transpose
1719
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {

Diff for: src/types-and-values/exercise.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Omitting `return` is covered later.
16+
#[allow(clippy::needless_return)]
1517
// ANCHOR: solution
1618
// ANCHOR: fib
1719
fn fib(n: u32) -> u32 {

Diff for: src/unsafe-rust/exercise.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#[allow(clippy::upper_case_acronyms)]
1516
// ANCHOR: solution
1617
// ANCHOR: ffi
1718
mod ffi {
@@ -40,7 +41,7 @@ mod ffi {
4041
}
4142

4243
// Layout according to the macOS man page for dir(5).
43-
#[cfg(all(target_os = "macos"))]
44+
#[cfg(target_os = "macos")]
4445
#[repr(C)]
4546
pub struct dirent {
4647
pub d_fileno: u64,

Diff for: third_party/rust-on-exercism/health-statistics.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@ impl User {
3737
patient_name: &self.name,
3838
visit_count: self.visit_count as u32,
3939
height_change: measurements.height - self.height,
40-
blood_pressure_change: match self.last_blood_pressure {
41-
Some(lbp) => {
42-
Some((bp.0 as i32 - lbp.0 as i32, bp.1 as i32 - lbp.1 as i32))
43-
}
44-
None => None,
45-
},
40+
blood_pressure_change: self
41+
.last_blood_pressure
42+
.map(|lbp| (bp.0 as i32 - lbp.0 as i32, bp.1 as i32 - lbp.1 as i32)),
4643
};
4744
self.height = measurements.height;
4845
self.last_blood_pressure = Some(bp);

0 commit comments

Comments
 (0)