Skip to content

Commit

Permalink
Codify lint toolchain, Mk.II (#1260)
Browse files Browse the repository at this point in the history
* Codify linting toolchain in a toolchain file

Moves the nightly toolchain we use for linting out of a GitHub action
and into a dedicated `toolchains/lint/rust-toolchain.toml` file.

rustup doesn't currently support specifying a toolchain file, though
[it will soon](rust-lang/rustup#2686), so we
hack around it by moving the toolchain file back and forth as part of
the GitHub action for Rust.

* Update code for recent clippy

* Update code for recent rustfmt

* Add a Nix devShell for the linting toolchain
  • Loading branch information
Twey authored Nov 22, 2023
1 parent 230e2c4 commit 5847d1c
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 40 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ permissions:
contents: read

jobs:

test:
runs-on: ubuntu-latest-16-cores
timeout-minutes: 45
Expand Down Expand Up @@ -103,11 +102,10 @@ jobs:
run: >
find linera-* -name '*.rs' -a -not -wholename '*/target/*' -print0
| xargs -0 -L1 ./scripts/target/release/check_copyright_header
- name: Put lint toolchain file in place
run: |
ln -sf toolchains/lint/rust-toolchain.toml
- uses: Twey/setup-rust-toolchain@v1
with:
toolchain: nightly-2023-10-22
target: wasm32-unknown-unknown
components: clippy rustfmt
- name: Install cargo-machete
run: |
cargo install cargo-machete --locked
Expand All @@ -126,7 +124,7 @@ jobs:
cargo install cargo-sort --locked
- name: Check formatting
run: |
cargo +nightly-2023-10-22 fmt -- --check
cargo fmt -- --check
- name: Check for unused dependencies
run: |
cargo machete
Expand Down Expand Up @@ -161,3 +159,7 @@ jobs:
run: |
cd examples
cargo sort -c -w
- name: Restore `rust-toolchain.toml` file
if: '!cancelled()'
run: |
ln -sf toolchains/build/rust-toolchain.toml
30 changes: 15 additions & 15 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
pkg-config
nodejs
];
rustToolchain = (pkgs.rust-bin.fromRustupToolchainFile
./rust-toolchain.toml);
rustPlatform = pkgs.makeRustPlatform {
rustc = rustToolchain;
cargo = rustToolchain;
};
rustBuildToolchain = (pkgs.rust-bin.fromRustupToolchainFile
./toolchains/build/rust-toolchain.toml);
rustLintToolchain = (pkgs.rust-bin.fromRustupToolchainFile
./toolchains/lint/rust-toolchain.toml);
in {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
Expand All @@ -45,17 +43,21 @@
];
shellHook = ''
# For rust-analyzer 'hover' tooltips to work.
export RUST_SRC_PATH=${rustToolchain.availableComponents.rust-src}
export RUST_SRC_PATH=${rustBuildToolchain.availableComponents.rust-src}
export LIBCLANG_PATH=${pkgs.libclang.lib}/lib
export PATH=$PWD/target/release:~/.cargo/bin:$PATH
'';
buildInputs = nonRustDeps;
nativeBuildInputs = with pkgs; [
rustToolchain
rustBuildToolchain
rust-analyzer
];
};

devShells.lint = pkgs.mkShell {
nativeBuildInputs = [ rustLintToolchain ];
};

# Add your auto-formatters here.
# cf. https://numtide.github.io/treefmt/
treefmt.config = {
Expand Down
2 changes: 1 addition & 1 deletion linera-core/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl CrossChainRequest {
CrossChainRequest::UpdateRecipient { height_map, .. } => {
height_map.iter().any(|(_, heights)| {
debug_assert!(heights.windows(2).all(|w| w[0] <= w[1]));
matches!(heights.get(0), Some(h) if *h <= height)
matches!(heights.first(), Some(h) if *h <= height)
})
}
_ => false,
Expand Down
2 changes: 1 addition & 1 deletion linera-explorer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ async fn chains(app: &JsValue, node: &str) -> Result<ChainId> {
.serialize(&SER)
.expect("failed to serialize ChainIds");
setf(app, "chains", &chains_js);
Ok(chains.default.unwrap_or_else(|| match chains.list.get(0) {
Ok(chains.default.unwrap_or_else(|| match chains.list.first() {
None => ChainId::from(ChainDescription::Root(0)),
Some(chain_id) => *chain_id,
}))
Expand Down
4 changes: 2 additions & 2 deletions linera-views-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn context_and_constraints(
context = Type::Path(TypePath {
qself: None,
path: template_vect
.get(0)
.first()
.expect("failed to find the first generic parameter")
.clone()
.into(),
Expand Down Expand Up @@ -126,7 +126,7 @@ fn generate_view_code(input: ItemStruct, root: bool) -> TokenStream2 {
clear_quotes.push(quote! { self.#name.clear(); });
}
let first_name_quote = name_quotes
.get(0)
.first()
.expect("list of names should be non-empty");

let increment_counter = if root {
Expand Down
6 changes: 2 additions & 4 deletions linera-views/src/queue_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ where
if count == 0 {
return Ok(Vec::new());
}
let mut values = Vec::new();
values.reserve(count);
let mut values = Vec::with_capacity(count);
if !self.was_cleared {
let stored_remainder = self.stored_count();
let start = self.stored_indices.end - stored_remainder;
Expand Down Expand Up @@ -336,8 +335,7 @@ where
if count == 0 {
return Ok(Vec::new());
}
let mut values = Vec::new();
values.reserve(count);
let mut values = Vec::with_capacity(count);
let new_back_len = self.new_back_values.len();
if count <= new_back_len || self.was_cleared {
values.extend(
Expand Down
12 changes: 9 additions & 3 deletions linera-views/tests/views_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,15 @@ async fn compute_hash_view_iter<R: RngCore>(rng: &mut R, n: usize, k: usize) {
.push(compute_hash_ordered_view(rng, &mut store3, key_value_vector.clone()).await);
}
for i in 1..n_iter {
assert_eq!(unord1_hashes.get(0).unwrap(), unord1_hashes.get(i).unwrap());
assert_eq!(unord2_hashes.get(0).unwrap(), unord2_hashes.get(i).unwrap());
assert_eq!(ord_hashes.get(0).unwrap(), ord_hashes.get(i).unwrap());
assert_eq!(
unord1_hashes.first().unwrap(),
unord1_hashes.get(i).unwrap(),
);
assert_eq!(
unord2_hashes.first().unwrap(),
unord2_hashes.get(i).unwrap(),
);
assert_eq!(ord_hashes.first().unwrap(), ord_hashes.get(i).unwrap());
}
}

Expand Down
5 changes: 5 additions & 0 deletions toolchains/build/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[toolchain]
channel = "1.73.0"
components = [ "clippy", "rustfmt", "rust-src" ]
targets = [ "wasm32-unknown-unknown" ]
profile = "minimal"
5 changes: 5 additions & 0 deletions toolchains/lint/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[toolchain]
channel = "nightly-2023-10-23"
components = [ "clippy", "rustfmt" ]
targets = [ "wasm32-unknown-unknown" ]
profile = "minimal"

0 comments on commit 5847d1c

Please sign in to comment.