Skip to content

Commit 5f988ab

Browse files
committed
Auto merge of rust-lang#2189 - RalfJung:clippy, r=RalfJung
run Clippy on CI and fix some things it complains about. Also use `rustup-toolchain` script on CI (reduces code duplication, and good thing to make sure it keeps working, since we recommend it in the docs). I left `ui_test` out for now; I'll leave those nits to `@oli-obk.` ;)
2 parents 5a1b09e + 3d30aec commit 5f988ab

File tree

8 files changed

+67
-66
lines changed

8 files changed

+67
-66
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,10 @@ jobs:
7272
shell: bash
7373
run: |
7474
if [[ ${{ github.event_name }} == 'schedule' ]]; then
75-
RUSTC_HASH=$(git ls-remote https://github.com/rust-lang/rust.git master | awk '{print $1}')
75+
./rustup-toolchain HEAD --host ${{ matrix.host_target }}
7676
else
77-
RUSTC_HASH=$(< rust-version)
77+
./rustup-toolchain "" --host ${{ matrix.host_target }}
7878
fi
79-
# We need a nightly cargo for parts of the cargo miri test suite.
80-
rustup-toolchain-install-master \
81-
-f \
82-
-n master "$RUSTC_HASH" \
83-
-c cargo \
84-
-c rust-src \
85-
-c rustc-dev \
86-
-c llvm-tools \
87-
--host ${{ matrix.host_target }}
88-
rustup default master
8979
9080
- name: Show Rust version
9181
run: |
@@ -97,26 +87,35 @@ jobs:
9787
run: bash ./ci.sh
9888

9989
fmt:
100-
name: check formatting (ignored by bors)
90+
name: formatting (ignored by bors)
10191
runs-on: ubuntu-latest
10292
steps:
10393
- uses: actions/checkout@v3
10494
- name: Install latest nightly
105-
uses: actions-rs/toolchain@v1
106-
with:
107-
toolchain: nightly
108-
components: rustfmt
109-
default: true
110-
- name: Check formatting (miri)
111-
uses: actions-rs/cargo@v1
112-
with:
113-
command: fmt
114-
args: --all -- --check
115-
- name: Check formatting (cargo-miri)
116-
uses: actions-rs/cargo@v1
117-
with:
118-
command: fmt
119-
args: --manifest-path cargo-miri/Cargo.toml --all -- --check
95+
run: |
96+
rustup toolchain install nightly --component rustfmt
97+
rustup override set nightly
98+
- name: Formatting (miri, ui_test)
99+
run: cargo fmt --all --check
100+
- name: Formatting (cargo-miri)
101+
run: cargo fmt --manifest-path cargo-miri/Cargo.toml --all --check
102+
103+
clippy:
104+
name: clippy (ignored by bors)
105+
runs-on: ubuntu-latest
106+
steps:
107+
- uses: actions/checkout@v3
108+
- name: Install required toolchain
109+
# We need a toolchain that can actually build Miri, just a nightly won't do.
110+
run: |
111+
cargo install rustup-toolchain-install-master # TODO: cache this?
112+
./rustup-toolchain "" -c clippy
113+
- name: Clippy (miri)
114+
run: cargo clippy --all-targets -- -D warnings
115+
#- name: Clippy (ui_test)
116+
# run: cargo clippy --manifest-path ui_test/Cargo.toml --all-targets -- -D warnings
117+
- name: Clippy (cargo-miri)
118+
run: cargo clippy --manifest-path cargo-miri/Cargo.toml --all-targets -- -D warnings
120119

121120
# These jobs doesn't actually test anything, but they're only used to tell
122121
# bors the build completed, as there is no practical way to detect when a

benches/helpers/fibonacci_helper_iterative.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn fib(n: usize) -> usize {
99
for _ in 0..n {
1010
let c = a;
1111
a = b;
12-
b = c + b;
12+
b += c;
1313
}
1414
a
1515
}

cargo-miri/bin.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::useless_format, clippy::derive_partial_eq_without_eq)]
2+
13
mod version;
24

35
use std::env;
@@ -96,6 +98,9 @@ fn show_version() {
9698
// Only use `option_env` on vergen variables to ensure the build succeeds
9799
// when vergen failed to find the git info.
98100
if let Some(sha) = option_env!("VERGEN_GIT_SHA_SHORT") {
101+
// This `unwrap` can never fail because if VERGEN_GIT_SHA_SHORT exists, then so does
102+
// VERGEN_GIT_COMMIT_DATE.
103+
#[allow(clippy::option_env_unwrap)]
99104
write!(&mut version, " ({} {})", sha, option_env!("VERGEN_GIT_COMMIT_DATE").unwrap())
100105
.unwrap();
101106
}
@@ -135,16 +140,14 @@ impl<I: Iterator<Item = String>> Iterator for ArgSplitFlagValue<'_, I> {
135140

136141
fn next(&mut self) -> Option<Self::Item> {
137142
let arg = self.args.next()?;
138-
if arg.starts_with(self.name) {
143+
if let Some(suffix) = arg.strip_prefix(self.name) {
139144
// Strip leading `name`.
140-
let suffix = &arg[self.name.len()..];
141145
if suffix.is_empty() {
142146
// This argument is exactly `name`; the next one is the value.
143147
return self.args.next().map(Ok);
144-
} else if suffix.starts_with('=') {
148+
} else if let Some(suffix) = suffix.strip_prefix('=') {
145149
// This argument is `name=value`; get the value.
146-
// Strip leading `=`.
147-
return Some(Ok(suffix[1..].to_owned()));
150+
return Some(Ok(suffix.to_owned()));
148151
}
149152
}
150153
Some(Err(arg))
@@ -255,7 +258,7 @@ fn xargo_version() -> Option<(u32, u32, u32)> {
255258
let line = out
256259
.stderr
257260
.lines()
258-
.nth(0)
261+
.next()
259262
.expect("malformed `xargo --version` output: not at least one line")
260263
.expect("malformed `xargo --version` output: error reading first line");
261264
let (name, version) = {
@@ -285,7 +288,7 @@ fn xargo_version() -> Option<(u32, u32, u32)> {
285288
.expect("malformed `xargo --version` output: not a patch version piece")
286289
.parse()
287290
.expect("malformed `xargo --version` output: patch version is not an integer");
288-
if !version_pieces.next().is_none() {
291+
if version_pieces.next().is_some() {
289292
panic!("malformed `xargo --version` output: more than three pieces in version");
290293
}
291294
Some((major, minor, patch))
@@ -311,7 +314,7 @@ fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
311314
println!("Running `{:?}` to {}.", cmd, text);
312315
}
313316

314-
if cmd.status().expect(&format!("failed to execute {:?}", cmd)).success().not() {
317+
if cmd.status().unwrap_or_else(|_| panic!("failed to execute {:?}", cmd)).success().not() {
315318
show_error(format!("failed to {}", text));
316319
}
317320
}
@@ -499,10 +502,11 @@ fn get_cargo_metadata() -> Metadata {
499502
for arg in ArgSplitFlagValue::new(
500503
env::args().skip(3), // skip the program name, "miri" and "run" / "test"
501504
config_flag,
502-
) {
503-
if let Ok(config) = arg {
504-
cmd.arg(config_flag).arg(config);
505-
}
505+
)
506+
// Only look at `Ok`
507+
.flatten()
508+
{
509+
cmd.arg(config_flag).arg(arg);
506510
}
507511
let mut child = cmd
508512
.stdin(process::Stdio::null())
@@ -524,11 +528,11 @@ fn get_cargo_metadata() -> Metadata {
524528
/// Additionally, somewhere between cargo metadata and TyCtxt, '-' gets replaced with '_' so we
525529
/// make that same transformation here.
526530
fn local_crates(metadata: &Metadata) -> String {
527-
assert!(metadata.workspace_members.len() > 0);
531+
assert!(!metadata.workspace_members.is_empty());
528532
let mut local_crates = String::new();
529533
for member in &metadata.workspace_members {
530-
let name = member.split(" ").nth(0).unwrap();
531-
let name = name.replace("-", "_");
534+
let name = member.split(' ').next().unwrap();
535+
let name = name.replace('-', "_");
532536
local_crates.push_str(&name);
533537
local_crates.push(',');
534538
}
@@ -708,7 +712,7 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
708712
get_arg_flag_value("--crate-name").unwrap(),
709713
// This is technically a `-C` flag but the prefix seems unique enough...
710714
// (and cargo passes this before the filename so it should be unique)
711-
get_arg_flag_value("extra-filename").unwrap_or(String::new()),
715+
get_arg_flag_value("extra-filename").unwrap_or_default(),
712716
suffix,
713717
));
714718
path
@@ -808,11 +812,10 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
808812
// Forward arguments, but remove "link" from "--emit" to make this a check-only build.
809813
let emit_flag = "--emit";
810814
while let Some(arg) = args.next() {
811-
if arg.starts_with(emit_flag) {
815+
if let Some(val) = arg.strip_prefix(emit_flag) {
812816
// Patch this argument. First, extract its value.
813-
let val = &arg[emit_flag.len()..];
814-
assert!(val.starts_with("="), "`cargo` should pass `--emit=X` as one argument");
815-
let val = &val[1..];
817+
let val =
818+
val.strip_prefix('=').expect("`cargo` should pass `--emit=X` as one argument");
816819
let mut val: Vec<_> = val.split(',').collect();
817820
// Now make sure "link" is not in there, but "metadata" is.
818821
if let Some(i) = val.iter().position(|&s| s == "link") {
@@ -937,12 +940,10 @@ fn phase_runner(binary: &Path, binary_args: env::Args, phase: RunnerPhase) {
937940
while let Some(arg) = args.next() {
938941
if arg == "--extern" {
939942
forward_patched_extern_arg(&mut args, &mut cmd);
940-
} else if arg.starts_with(error_format_flag) {
941-
let suffix = &arg[error_format_flag.len()..];
943+
} else if let Some(suffix) = arg.strip_prefix(error_format_flag) {
942944
assert!(suffix.starts_with('='));
943945
// Drop this argument.
944-
} else if arg.starts_with(json_flag) {
945-
let suffix = &arg[json_flag.len()..];
946+
} else if let Some(suffix) = arg.strip_prefix(json_flag) {
946947
assert!(suffix.starts_with('='));
947948
// Drop this argument.
948949
} else {

rustup-toolchain

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ set -e
1212
# ./rustup-toolchain HEAD: Update "miri" toolchain and `rust-version` file to latest rustc HEAD.
1313
#
1414
# ./rustup-toolchain $COMMIT: Update "miri" toolchain and `rust-version` file to match that commit.
15+
#
16+
# Any extra parameters are passed to `rustup-toolchain-install-master`.
1517

1618
# Make sure rustup-toolchain-install-master is installed.
1719
if ! which rustup-toolchain-install-master >/dev/null; then
@@ -28,6 +30,7 @@ else
2830
NEW_COMMIT="$1"
2931
fi
3032
echo "$NEW_COMMIT" > rust-version
33+
shift
3134

3235
# Check if we already are at that commit.
3336
CUR_COMMIT=$(rustc +miri --version -v 2>/dev/null | egrep "^commit-hash: " | cut -d " " -f 2)
@@ -39,7 +42,7 @@ fi
3942

4043
# Install and setup new toolchain.
4144
rustup toolchain uninstall miri
42-
rustup-toolchain-install-master -n miri -c cargo -c rust-src -c rustc-dev -c llvm-tools -- "$NEW_COMMIT"
45+
rustup-toolchain-install-master -n miri -c cargo -c rust-src -c rustc-dev -c llvm-tools "$@" -- "$NEW_COMMIT"
4346
rustup override set miri
4447

4548
# Cleanup.

src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
#![feature(io_error_more)]
88
#![warn(rust_2018_idioms)]
99
#![allow(
10-
clippy::cast_lossless,
1110
clippy::collapsible_else_if,
1211
clippy::collapsible_if,
1312
clippy::comparison_chain,
1413
clippy::enum_variant_names,
1514
clippy::field_reassign_with_default,
16-
clippy::from_over_into,
17-
clippy::if_same_then_else,
1815
clippy::manual_map,
19-
clippy::needless_lifetimes,
2016
clippy::new_without_default,
2117
clippy::single_match,
22-
clippy::useless_format
18+
clippy::useless_format,
19+
clippy::derive_partial_eq_without_eq
2320
)]
2421

2522
extern crate rustc_apfloat;

src/machine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ pub enum MiriMemoryKind {
8989
Tls,
9090
}
9191

92-
impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
92+
impl From<MiriMemoryKind> for MemoryKind<MiriMemoryKind> {
9393
#[inline(always)]
94-
fn into(self) -> MemoryKind<MiriMemoryKind> {
95-
MemoryKind::Machine(self)
94+
fn from(kind: MiriMemoryKind) -> MemoryKind<MiriMemoryKind> {
95+
MemoryKind::Machine(kind)
9696
}
9797
}
9898

src/shims/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ fn bool_to_simd_element(b: bool, size: Size) -> Scalar<Tag> {
13891389
Scalar::from_int(val, size)
13901390
}
13911391

1392-
fn simd_element_to_bool<'tcx>(elem: ImmTy<'tcx, Tag>) -> InterpResult<'tcx, bool> {
1392+
fn simd_element_to_bool(elem: ImmTy<'_, Tag>) -> InterpResult<'_, bool> {
13931393
let val = elem.to_scalar()?.to_int(elem.layout.size)?;
13941394
Ok(match val {
13951395
0 => false,

src/shims/posix/sync.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
535535
throw_ub_format!(
536536
"unlocked a PTHREAD_MUTEX_NORMAL mutex that was not locked by the current thread"
537537
);
538-
} else if kind == this.eval_libc("PTHREAD_MUTEX_ERRORCHECK")? {
539-
this.eval_libc_i32("EPERM")
540-
} else if kind == this.eval_libc("PTHREAD_MUTEX_RECURSIVE")? {
538+
} else if kind == this.eval_libc("PTHREAD_MUTEX_ERRORCHECK")?
539+
|| kind == this.eval_libc("PTHREAD_MUTEX_RECURSIVE")?
540+
{
541541
this.eval_libc_i32("EPERM")
542542
} else {
543543
throw_unsup_format!("called pthread_mutex_unlock on an unsupported type of mutex");
@@ -642,6 +642,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
642642
let id = rwlock_get_or_create_id(this, rwlock_op)?;
643643
let active_thread = this.get_active_thread();
644644

645+
#[allow(clippy::if_same_then_else)]
645646
if this.rwlock_reader_unlock(id, active_thread) {
646647
Ok(0)
647648
} else if this.rwlock_writer_unlock(id, active_thread) {

0 commit comments

Comments
 (0)