Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utils: Add scope config for visitors and add for_each_expr fn #232

Merged
merged 4 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

14 changes: 7 additions & 7 deletions cargo-marker/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,27 @@ mod tests {

#[test]
fn test_marker_cli() {
let cli = MarkerCli::parse_from(&["cargo-marker", "check"]);
let cli = MarkerCli::parse_from(["cargo-marker", "check"]);
assert!(matches!(cli.command, Some(CliCommand::Check(_))));

let cli = MarkerCli::parse_from(&["cargo-marker"]);
assert!(matches!(cli.command, None));
let cli = MarkerCli::parse_from(["cargo-marker"]);
assert!(cli.command.is_none());
assert!(cli.check_args.cargo_args.is_empty());

let cli = MarkerCli::parse_from(&["cargo-marker", "--", "ducks", "penguins"]);
assert!(matches!(cli.command, None));
let cli = MarkerCli::parse_from(["cargo-marker", "--", "ducks", "penguins"]);
assert!(cli.command.is_none());
assert!(cli.check_args.cargo_args.len() == 2);
assert!(cli.check_args.cargo_args[0] == "ducks");
assert!(cli.check_args.cargo_args[1] == "penguins");

let cli = MarkerCli::parse_from(&["cargo-marker", "check", "--", "ducks", "penguins"]);
let cli = MarkerCli::parse_from(["cargo-marker", "check", "--", "ducks", "penguins"]);
assert!(cli.check_args.cargo_args.is_empty());
if let Some(CliCommand::Check(check_args)) = cli.command {
assert!(check_args.cargo_args.len() == 2);
assert!(check_args.cargo_args[0] == "ducks");
assert!(check_args.cargo_args[1] == "penguins");
} else {
assert!(false, "the `check` subcommand was not detected");
panic!("the `check` subcommand was not detected");
}
}
}
4 changes: 4 additions & 0 deletions marker_adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ impl Adapter {
}

impl Visitor<()> for AdapterInner {
fn scope(&self) -> visitor::VisitorScope {
visitor::VisitorScope::AllBodies
}

fn visit_item<'ast>(&mut self, cx: &'ast AstContext<'ast>, item: ItemKind<'ast>) -> ControlFlow<()> {
self.external_lint_crates.check_item(cx, item);
ControlFlow::Continue(())
Expand Down
1 change: 1 addition & 0 deletions marker_api/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use crate::ast::ty::SynTyData;

// Common types
pub use crate::ast::expr::ExprKind;
pub use crate::ast::item::Body;
pub use crate::ast::item::ItemKind;
pub use crate::ast::pat::PatKind;
pub use crate::ast::stmt::StmtKind;
Expand Down
3 changes: 2 additions & 1 deletion marker_uilints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ version = { workspace = true }
crate-type = ["cdylib"]

[dependencies]
marker_api = { workspace = true }
marker_api = { workspace = true }
marker_utils = { workspace = true }

[dev-dependencies]
marker_uitest = { workspace = true }
Expand Down
12 changes: 11 additions & 1 deletion marker_uilints/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![doc = include_str!("../README.md")]
#![warn(clippy::pedantic)]

mod utils;

use marker_api::{
ast::{
item::{EnumVariant, Field, StaticItem},
Expand Down Expand Up @@ -66,10 +68,18 @@ fn emit_item_with_test_name_lint<'ast>(

impl LintPass for TestLintPass {
fn info(&self) -> LintPassInfo {
LintPassInfoBuilder::new(Box::new([TEST_LINT, ITEM_WITH_TEST_NAME, PRINT_EVERY_EXPR])).build()
LintPassInfoBuilder::new(Box::new([
TEST_LINT,
ITEM_WITH_TEST_NAME,
PRINT_EVERY_EXPR,
utils::TEST_CONTAINS_RETURN,
]))
.build()
}

fn check_item<'ast>(&mut self, cx: &'ast AstContext<'ast>, item: ItemKind<'ast>) {
utils::check_item(cx, item);

if let ItemKind::Fn(item) = item {
if let Some(ident) = item.ident() {
if ident.name() == "test_ty_id_resolution_trigger" {
Expand Down
27 changes: 27 additions & 0 deletions marker_uilints/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use marker_api::prelude::*;
use marker_utils::visitor::BoolTraversable;

marker_api::declare_lint! {
/// # What it does
/// Tests the [`marker_utils::search::contains_return`] function.
TEST_CONTAINS_RETURN,
Warn,
}

pub fn check_item<'ast>(cx: &'ast AstContext<'ast>, item: ItemKind<'ast>) {
let ItemKind::Fn(fn_item) = item else { return };
let Some(ident) = fn_item.ident() else { return };

if ident.name().starts_with("test_contains_return") {
let body = cx.body(fn_item.body_id().unwrap());
let res = body.contains_return(cx);

cx.emit_lint(
TEST_CONTAINS_RETURN,
item.id(),
format!("testing `contains_return` -> {res}"),
ident.span(),
|_| {},
);
}
}
32 changes: 32 additions & 0 deletions marker_uilints/tests/ui/utils/contains_return.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![allow(unused)]

fn test_contains_return_1_false() {
// False, doesn't contain anything
}

fn test_contains_return_2_false() {
if (16 << 2) < 2 {
println!("IDK");
} else {
println!("idk");
}
}

fn test_contains_return_3_true() {
// True, is this code useful? Nope, but it contains a return
return;
}

fn test_contains_return_4_true() -> Option<u32> {
// True, is this code useful? Still nope, somehow it's worse
let x: u32 = Some(2)?;
Some(x)
}

fn test_contains_return_5_false() {
// False, the return is nested
fn nested_function() {
return;
}
nested_function();
}
34 changes: 34 additions & 0 deletions marker_uilints/tests/ui/utils/contains_return.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
warning: testing `contains_return` -> false
--> $DIR/contains_return.rs:3:4
|
3 | fn test_contains_return_1_false() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(marker::test_contains_return)]` on by default

warning: testing `contains_return` -> false
--> $DIR/contains_return.rs:7:4
|
7 | fn test_contains_return_2_false() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: testing `contains_return` -> true
--> $DIR/contains_return.rs:15:4
|
15 | fn test_contains_return_3_true() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: testing `contains_return` -> true
--> $DIR/contains_return.rs:20:4
|
20 | fn test_contains_return_4_true() -> Option<u32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: testing `contains_return` -> false
--> $DIR/contains_return.rs:26:4
|
26 | fn test_contains_return_5_false() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: 5 warnings emitted

Loading