1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-10-31 08:37:45 +02:00

Introduce a cargo clippy run (#2025)

This might help catch stylistic problems in our Rust code.

Related to #2587.
This commit is contained in:
Martin Geisler
2025-09-20 13:38:54 +02:00
committed by GitHub
parent 80a2f2ff71
commit 1e3be175fa
13 changed files with 81 additions and 57 deletions

View File

@@ -11,38 +11,6 @@ env:
CARGO_TERM_COLOR: always
jobs:
format:
runs-on: ubuntu-latest
env:
NIGHTLY_VERSION: nightly-2025-09-01
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Install formatting dependencies
run: |
sudo apt update
sudo apt install gettext yapf3
- name: Install pinned nightly for rustfmt
run: |
rustup toolchain install --profile minimal "$NIGHTLY_VERSION"
rustup component add rustfmt --toolchain "$NIGHTLY_VERSION"
- name: Check formatting
uses: dprint/check@v2.3
typos:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Check for typos
uses: crate-ci/typos@v1.35.7
with:
config: ./.github/typos.toml
cargo:
strategy:
matrix:

55
.github/workflows/lint.yml vendored Normal file
View File

@@ -0,0 +1,55 @@
name: Lint
on:
pull_request:
push:
branches:
- main
env:
CARGO_TERM_COLOR: always
jobs:
clippy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Setup Rust cache
uses: ./.github/workflows/setup-rust-cache
- name: Clippy
run: cargo clippy -- -Dwarnings
format:
runs-on: ubuntu-latest
env:
NIGHTLY_VERSION: nightly-2025-09-01
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Install formatting dependencies
run: |
sudo apt update
sudo apt install gettext yapf3
- name: Install pinned nightly for rustfmt
run: |
rustup toolchain install --profile minimal "$NIGHTLY_VERSION"
rustup component add rustfmt --toolchain "$NIGHTLY_VERSION"
- name: Check formatting
uses: dprint/check@v2.3
typos:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Check for typos
uses: crate-ci/typos@v1.35.7
with:
config: ./.github/typos.toml

View File

@@ -48,7 +48,7 @@ fn timediff(actual: u64, target: u64, slop: u64) -> String {
} else if actual + slop < target {
format!("{}: ({} short)", duration(actual), duration(target - actual),)
} else {
format!("{}", duration(actual))
duration(actual).to_string()
}
}

View File

@@ -29,7 +29,7 @@ fn main() {
);
let matches = app.get_matches();
if let Some(_) = matches.subcommand_matches("supports") {
if matches.subcommand_matches("supports").is_some() {
// Support all renderers.
process::exit(0);
}

View File

@@ -156,7 +156,7 @@ impl Courses {
fn course_mut(&mut self, name: impl AsRef<str>) -> &mut Course {
let name = name.as_ref();
if let Some(found_idx) =
self.courses.iter().position(|course| &course.name == name)
self.courses.iter().position(|course| course.name == name)
{
return &mut self.courses[found_idx];
}
@@ -177,9 +177,7 @@ impl Courses {
&self,
chapter: &Chapter,
) -> Option<(&Course, &Session, &Segment, &Slide)> {
let Some(ref source_path) = chapter.source_path else {
return None;
};
let source_path = chapter.source_path.as_ref()?;
for course in self {
for session in course {
@@ -193,7 +191,7 @@ impl Courses {
}
}
return None;
None
}
}
@@ -202,7 +200,7 @@ impl<'a> IntoIterator for &'a Courses {
type IntoIter = std::slice::Iter<'a, Course>;
fn into_iter(self) -> Self::IntoIter {
(&self.courses).into_iter()
self.courses.iter()
}
}
@@ -216,7 +214,7 @@ impl Course {
fn session_mut(&mut self, name: impl AsRef<str>) -> &mut Session {
let name = name.as_ref();
if let Some(found_idx) =
self.sessions.iter().position(|session| &session.name == name)
self.sessions.iter().position(|session| session.name == name)
{
return &mut self.sessions[found_idx];
}
@@ -275,7 +273,7 @@ impl<'a> IntoIterator for &'a Course {
type IntoIter = std::slice::Iter<'a, Session>;
fn into_iter(self) -> Self::IntoIter {
(&self.sessions).into_iter()
self.sessions.iter()
}
}
@@ -348,7 +346,7 @@ impl<'a> IntoIterator for &'a Session {
type IntoIter = std::slice::Iter<'a, Segment>;
fn into_iter(self) -> Self::IntoIter {
(&self.segments).into_iter()
self.segments.iter()
}
}
@@ -401,7 +399,7 @@ impl<'a> IntoIterator for &'a Segment {
type IntoIter = std::slice::Iter<'a, Slide>;
fn into_iter(self) -> Self::IntoIter {
(&self.slides).into_iter()
self.slides.iter()
}
}
@@ -449,7 +447,7 @@ impl Slide {
pub fn is_sub_chapter(&self, chapter: &Chapter) -> bool {
// The first `source_path` in the slide is the "parent" chapter, so anything
// else is a sub-chapter.
chapter.source_path.as_ref() != self.source_paths.get(0)
chapter.source_path.as_ref() != self.source_paths.first()
}
/// Return the total duration of this slide.

View File

@@ -83,7 +83,7 @@ impl<const N: usize> Table<N> {
for cell in iter {
write!(f, " {} |", cell)?;
}
write!(f, "\n")
writeln!(f)
}
}

View File

@@ -51,7 +51,7 @@ pub fn replace(
["course", "outline", course_name @ ..] => {
let course_name = course_name.join(" ");
let Some(course) = courses.find_course(course_name) else {
return format!("not found - {}", captures[0].to_string());
return format!("not found - {}", &captures[0]);
};
course.schedule()
}

View File

@@ -41,7 +41,7 @@ async fn main() -> Result<(), tokio_websockets::Error> {
println!("From server: {}", text);
}
},
Some(Err(err)) => return Err(err.into()),
Some(Err(err)) => return Err(err),
None => return Ok(()),
}
}

View File

@@ -116,7 +116,7 @@ fn unpack_tag(tag: u64) -> (u64, WireType) {
// ANCHOR: parse_field
/// Parse a field, returning the remaining bytes
fn parse_field(data: &[u8]) -> (Field, &[u8]) {
fn parse_field(data: &[u8]) -> (Field<'_>, &[u8]) {
let (tag, remainder) = parse_varint(data);
let (field_num, wire_type) = unpack_tag(tag);
let (fieldvalue, remainder) = match wire_type {

View File

@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Iterators are covered later.
#[allow(clippy::needless_range_loop)]
// ANCHOR: solution
// ANCHOR: transpose
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {

View File

@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Omitting `return` is covered later.
#[allow(clippy::needless_return)]
// ANCHOR: solution
// ANCHOR: fib
fn fib(n: u32) -> u32 {

View File

@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// `DIR` matches name of C struct.
#[allow(clippy::upper_case_acronyms)]
// ANCHOR: solution
// ANCHOR: ffi
mod ffi {
@@ -40,7 +42,7 @@ mod ffi {
}
// Layout according to the macOS man page for dir(5).
#[cfg(all(target_os = "macos"))]
#[cfg(target_os = "macos")]
#[repr(C)]
pub struct dirent {
pub d_fileno: u64,

View File

@@ -29,7 +29,7 @@ impl User {
// ANCHOR_END: setup
// ANCHOR: User_visit_doctor
pub fn visit_doctor(&mut self, measurements: Measurements) -> HealthReport {
pub fn visit_doctor(&mut self, measurements: Measurements) -> HealthReport<'_> {
// ANCHOR_END: User_visit_doctor
self.visit_count += 1;
let bp = measurements.blood_pressure;
@@ -37,12 +37,9 @@ impl User {
patient_name: &self.name,
visit_count: self.visit_count,
height_change: measurements.height - self.height,
blood_pressure_change: match self.last_blood_pressure {
Some(lbp) => {
Some((bp.0 as i32 - lbp.0 as i32, bp.1 as i32 - lbp.1 as i32))
}
None => None,
},
blood_pressure_change: self
.last_blood_pressure
.map(|lbp| (bp.0 as i32 - lbp.0 as i32, bp.1 as i32 - lbp.1 as i32)),
};
self.height = measurements.height;
self.last_blood_pressure = Some(bp);