Skip to content

Commit

Permalink
feat(lints): added proper ast-grep rules for checking println and
Browse files Browse the repository at this point in the history
unwraps
  • Loading branch information
uttarayan21 committed Nov 5, 2024
1 parent ed8bf80 commit 0289b0e
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 24 deletions.
1 change: 1 addition & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mnn-sys/vendor
11 changes: 0 additions & 11 deletions .rules/no-println.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions .rules/no-unwrap.yml

This file was deleted.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
partitionType = "count";
cargoExtraArgs = "-p mnn-sys";
});
# mnn-lints = sgLib.scan ./.;
mnn-lints = sgLib.scan ./.;
# mnn-asan = let
# rustPlatform = pkgs.makeRustPlatform {
# cargo = nightlyToolchain;
Expand Down
6 changes: 5 additions & 1 deletion sgconfig.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
ruleDirs:
- ./.rules
- ./tools/sg-lints/lints
utilDirs:
- ./tools/sg-lints/utils
ignores:
- mnn-sys/vendor
6 changes: 3 additions & 3 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl Interpreter {
let path = path.as_ref();
crate::ensure!(path.exists(), ErrorKind::IOError);
let path = path.to_str().ok_or_else(|| error!(ErrorKind::AsciiError))?;
let c_path = std::ffi::CString::new(path).unwrap();
let c_path = std::ffi::CString::new(path).change_context(ErrorKind::AsciiError)?;
unsafe { mnn_sys::modelPrintIO(c_path.as_ptr()) }
Ok(())
}
Expand Down Expand Up @@ -384,7 +384,7 @@ impl Interpreter {
name: impl AsRef<str>,
) -> Tensor<RefMut<'s, Device<H>>> {
let name = name.as_ref();
let c_name = std::ffi::CString::new(name).unwrap();
let c_name = std::ffi::CString::new(name).change_context(ErrorKind::AsciiError)?;
let input =
mnn_sys::Interpreter_getSessionInput(self.inner, session.inner, c_name.as_ptr());
Tensor::from_ptr(input)
Expand Down Expand Up @@ -866,7 +866,7 @@ fn check_whether_sync_actually_works() {
}

#[test]
#[ignore = "This test doesn't work in CI"]
#[ignore = "Fails on CI"]
fn try_to_drop_interpreter_before_session() {
let file = Path::new("tests/assets/realesr.mnn")
.canonicalize()
Expand Down
22 changes: 22 additions & 0 deletions tools/sg-lints/lints/no-println.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
id: no-println
message: Do not use println! use `tracing::info`/`tracing::trace`/`tracing::debug` instead
severity: warning
language: Rust
rule:
kind: macro_invocation
pattern: println!($$$ITEMS)
not:
inside:
stopBy: end
matches: is-test

fix: tracing::info!($$$ITEMS)
files:
- src/**/*.rs
- mnn-sync/src/*.rs
- mnn-sys/src/*.rs
- mnn-bridge/src/**/*.rs
ignores:
- build.rs
- mnn-sys/build.rs
- mnn-sys/vendor/**/*.rs
19 changes: 19 additions & 0 deletions tools/sg-lints/lints/no-unwrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
id: no-unwrap
message: Do not use unwrap
severity: error
language: Rust
rule:
pattern: $ITEM.unwrap()
not:
inside:
stopBy: end
matches: is-test
files:
- src/**/*.rs
- mnn-sync/src/*.rs
- mnn-sys/src/*.rs
- mnn-bridge/src/**/*.rs
ignores:
- build.rs
- mnn-sys/vendor/**/*.rs

23 changes: 23 additions & 0 deletions tools/sg-lints/utils/is-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
id: is-test
language: Rust

rule:
all:
- kind: function_item
- follows:
stopBy:
kind: function_item
matches: test-token

utils:
test-token:
kind: attribute_item
has:
kind: attribute
has:
any:
- pattern: test
- pattern: tokio::test

ignores:
- mnn-sys/vendor/**/*.rs

0 comments on commit 0289b0e

Please sign in to comment.