Skip to content

Commit 1e3be17

Browse files
authored
Introduce a cargo clippy run (#2025)
This might help catch stylistic problems in our Rust code. Related to #2587.
1 parent 80a2f2f commit 1e3be17

File tree

13 files changed

+81
-57
lines changed

13 files changed

+81
-57
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,6 @@ env:
1111
CARGO_TERM_COLOR: always
1212

1313
jobs:
14-
format:
15-
runs-on: ubuntu-latest
16-
env:
17-
NIGHTLY_VERSION: nightly-2025-09-01
18-
steps:
19-
- name: Checkout
20-
uses: actions/checkout@v5
21-
22-
- name: Install formatting dependencies
23-
run: |
24-
sudo apt update
25-
sudo apt install gettext yapf3
26-
27-
- name: Install pinned nightly for rustfmt
28-
run: |
29-
rustup toolchain install --profile minimal "$NIGHTLY_VERSION"
30-
rustup component add rustfmt --toolchain "$NIGHTLY_VERSION"
31-
32-
- name: Check formatting
33-
uses: dprint/[email protected]
34-
35-
typos:
36-
runs-on: ubuntu-latest
37-
steps:
38-
- name: Checkout
39-
uses: actions/checkout@v5
40-
41-
- name: Check for typos
42-
uses: crate-ci/[email protected]
43-
with:
44-
config: ./.github/typos.toml
45-
4614
cargo:
4715
strategy:
4816
matrix:

.github/workflows/lint.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
clippy:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v5
18+
19+
- name: Setup Rust cache
20+
uses: ./.github/workflows/setup-rust-cache
21+
22+
- name: Clippy
23+
run: cargo clippy -- -Dwarnings
24+
25+
format:
26+
runs-on: ubuntu-latest
27+
env:
28+
NIGHTLY_VERSION: nightly-2025-09-01
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v5
32+
33+
- name: Install formatting dependencies
34+
run: |
35+
sudo apt update
36+
sudo apt install gettext yapf3
37+
38+
- name: Install pinned nightly for rustfmt
39+
run: |
40+
rustup toolchain install --profile minimal "$NIGHTLY_VERSION"
41+
rustup component add rustfmt --toolchain "$NIGHTLY_VERSION"
42+
43+
- name: Check formatting
44+
uses: dprint/[email protected]
45+
46+
typos:
47+
runs-on: ubuntu-latest
48+
steps:
49+
- name: Checkout
50+
uses: actions/checkout@v5
51+
52+
- name: Check for typos
53+
uses: crate-ci/[email protected]
54+
with:
55+
config: ./.github/typos.toml

mdbook-course/src/bin/course-schedule.rs

Lines changed: 1 addition & 1 deletion
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

mdbook-course/src/bin/mdbook-course.rs

Lines changed: 1 addition & 1 deletion
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
}

mdbook-course/src/course.rs

Lines changed: 9 additions & 11 deletions
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

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

350348
fn into_iter(self) -> Self::IntoIter {
351-
(&self.segments).into_iter()
349+
self.segments.iter()
352350
}
353351
}
354352

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

403401
fn into_iter(self) -> Self::IntoIter {
404-
(&self.slides).into_iter()
402+
self.slides.iter()
405403
}
406404
}
407405

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

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

mdbook-course/src/markdown.rs

Lines changed: 1 addition & 1 deletion
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

mdbook-course/src/replacements.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn replace(
5151
["course", "outline", course_name @ ..] => {
5252
let course_name = course_name.join(" ");
5353
let Some(course) = courses.find_course(course_name) else {
54-
return format!("not found - {}", captures[0].to_string());
54+
return format!("not found - {}", &captures[0]);
5555
};
5656
course.schedule()
5757
}

src/concurrency/async-exercises/chat-async/src/bin/client.rs

Lines changed: 1 addition & 1 deletion
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
}

src/lifetimes/exercise.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn unpack_tag(tag: u64) -> (u64, WireType) {
116116

117117
// ANCHOR: parse_field
118118
/// Parse a field, returning the remaining bytes
119-
fn parse_field(data: &[u8]) -> (Field, &[u8]) {
119+
fn parse_field(data: &[u8]) -> (Field<'_>, &[u8]) {
120120
let (tag, remainder) = parse_varint(data);
121121
let (field_num, wire_type) = unpack_tag(tag);
122122
let (fieldvalue, remainder) = match wire_type {

src/tuples-and-arrays/exercise.rs

Lines changed: 2 additions & 0 deletions
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] {

0 commit comments

Comments
 (0)