Skip to content

Commit a654f7e

Browse files
authored
Support file formatting and linting (#38)
This PR will add code quality checks to CI through [cargo clippy](https://doc.rust-lang.org/clippy/index.html) and [cargo fmt](https://github.com/rust-lang/rustfmt) Closes #37
1 parent d213c8c commit a654f7e

File tree

9 files changed

+1073
-791
lines changed

9 files changed

+1073
-791
lines changed

.github/workflows/test.yml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,38 @@ env:
1313
CARGO_TERM_COLOR: always
1414

1515
jobs:
16+
code-quality:
17+
name: "Code Quality"
18+
runs-on: [self-hosted, linux, normal]
19+
steps:
20+
- name: 'Check out code'
21+
uses: actions/checkout@v4
22+
with:
23+
# Check out pull request HEAD instead of merge commit.
24+
ref: ${{ github.event.pull_request.head.sha }}
25+
submodules: recursive
26+
27+
- name: "Set up nightly Rust" # https://github.com/rust-lang/rustup/issues/3409
28+
uses: dtolnay/rust-toolchain@master
29+
with:
30+
toolchain: nightly-2024-11-29 # Hardcoded version, same as is in the build.rs
31+
32+
- name: 'Build smir_pretty' # rustfmt documentation claims it is unstable on code that doesn't build
33+
run: |
34+
cargo build -vv
35+
36+
- name: "Check `cargo clippy`"
37+
run: |
38+
rustup component add clippy
39+
cargo clippy -- -Dwarnings
40+
41+
- name: "Check `cargo fmt`"
42+
run: |
43+
rustup component add rustfmt
44+
cargo fmt --check
1645
1746
integration-tests:
47+
needs: code-quality
1848
name: "Integration tests"
1949
runs-on: [self-hosted, linux, normal]
2050
steps:
@@ -31,8 +61,8 @@ jobs:
3161
toolchain: nightly-2024-11-29 # Hardcoded version, same as is in the build.rs
3262

3363
- name: 'Build smir_pretty'
34-
run: |
35-
cargo build -vv
64+
run: | # Warning check should be redundant since code-quality runs first
65+
RUSTFLAGS='--deny warnings' cargo build -vv
3666
3767
- name: 'Run smir integration tests'
3868
run: |

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ There are a few environment variables that can be set to control the tools outpu
3636
2. `LINK_INST` - use a richer key-structure for the link-time `functions` map which uses keys that are pairs of a function type (`Ty`) _and_ an function instance kind (`InstanceKind`)
3737
3. `DEBUG` - serialize additional data in the JSON file and dump logs to stdout
3838

39+
## Development
40+
41+
To ensure code quality, all code is required to pass `cargo clippy` and `cargo fmt` without warning to pass CI.
42+
3943
## Tests
4044

4145
### Running the Tests

build.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@ use std::process::Command;
22

33
fn main() {
44
let status = Command::new("rustup")
5-
.args(&["install", "nightly-2024-11-29"])
5+
.args(["install", "nightly-2024-11-29"])
66
.status()
77
.expect("build.rs failed to install nightly-2024-11-29");
88

99
println!("Installed nightly-2024-11-29: {}", status);
1010

1111
let status = Command::new("rustup")
12-
.args(&["default", "nightly-2024-11-29"])
12+
.args(["default", "nightly-2024-11-29"])
1313
.status()
1414
.expect("build.rs failed to default nightly-2024-11-29");
1515

1616
println!("Defaulted nightly-2024-11-29: {}", status);
1717

1818
let status = Command::new("rustup")
19-
.args(&["component", "add", "rustc-dev"])
19+
.args(["component", "add", "rustc-dev"])
2020
.status()
2121
.expect("build.rs failed to install rustc-dev");
2222

2323
println!("Added component rustc-dev: {}", status);
2424

2525
let status = Command::new("rustup")
26-
.args(&["component", "add", "llvm-tools"])
26+
.args(["component", "add", "llvm-tools"])
2727
.status()
2828
.expect("build.rs failed to install llvm-tools");
2929

3030
println!("Added component llvm-tools: {}", status);
31-
}
31+
}

src/driver.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,32 @@
1717
//!
1818
//! However, we prefer a non-macro version for clarity and build simplicity.
1919
20-
extern crate rustc_middle;
2120
extern crate rustc_driver;
2221
extern crate rustc_interface;
23-
extern crate rustc_smir;
22+
extern crate rustc_middle;
2423
extern crate rustc_session;
25-
use rustc_middle::ty::TyCtxt;
24+
extern crate rustc_smir;
2625
use rustc_driver::Compilation;
2726
use rustc_interface::interface::Compiler;
27+
use rustc_middle::ty::TyCtxt;
2828
use rustc_smir::rustc_internal;
2929

3030
struct StableMirCallbacks {
31-
callback_fn: fn (TyCtxt) -> (),
31+
callback_fn: fn(TyCtxt) -> (),
3232
}
3333

3434
impl rustc_driver::Callbacks for StableMirCallbacks {
35-
fn after_analysis<'tcx>(
36-
&mut self,
37-
_compiler: &Compiler,
38-
tcx: TyCtxt<'tcx>,
39-
) -> Compilation {
40-
35+
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt) -> Compilation {
4136
let _ = rustc_internal::run(tcx, || (self.callback_fn)(tcx));
4237

4338
Compilation::Continue
4439
}
4540
}
4641

47-
pub fn stable_mir_driver(args_outer: &Vec<String>, callback_fn: fn (TyCtxt) -> ()) {
42+
pub fn stable_mir_driver(args_outer: &[String], callback_fn: fn(TyCtxt) -> ()) {
4843
let mut callbacks = StableMirCallbacks { callback_fn };
49-
let early_dcx = rustc_session::EarlyDiagCtxt::new(rustc_session::config::ErrorOutputType::default());
44+
let early_dcx =
45+
rustc_session::EarlyDiagCtxt::new(rustc_session::config::ErrorOutputType::default());
5046
rustc_driver::init_rustc_env_logger(&early_dcx);
5147
let _ = rustc_driver::RunCompiler::new(args_outer, &mut callbacks).run();
5248
}

src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ fn main() {
1010
let mut args: Vec<String> = env::args().collect();
1111

1212
match args.get(1) {
13-
None =>
14-
stable_mir_driver(&args, emit_smir), // backward compatibility
13+
None => stable_mir_driver(&args, emit_smir), // backward compatibility
1514
Some(arg) if arg == "--json" => {
1615
args.remove(1);
1716
stable_mir_driver(&args, emit_smir)
@@ -20,7 +19,6 @@ fn main() {
2019
args.remove(1);
2120
stable_mir_driver(&args, emit_dotfile)
2221
}
23-
Some(_other) =>
24-
stable_mir_driver(&args, emit_smir), // backward compatibility
22+
Some(_other) => stable_mir_driver(&args, emit_smir), // backward compatibility
2523
}
2624
}

0 commit comments

Comments
 (0)