-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utils: Add
for_each_expr
and contains_return
utils
- Loading branch information
Showing
10 changed files
with
202 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use marker_api::prelude::*; | ||
use marker_utils::search::contains_return; | ||
|
||
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 res = contains_return(cx, cx.body(fn_item.body_id().unwrap()).expr()); | ||
|
||
cx.emit_lint( | ||
TEST_CONTAINS_RETURN, | ||
item.id(), | ||
format!("testing `contains_return` -> {res}"), | ||
ident.span(), | ||
|_| {}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! This module contains utilities to search a given part of the AST for a | ||
//! different things. | ||
use std::ops::ControlFlow; | ||
|
||
use crate::visitor::{for_each_expr, Traverseable}; | ||
|
||
use marker_api::prelude::*; | ||
|
||
/// Checks if the given node contains an early return, in the form of an | ||
/// [`ReturnExpr`](marker_api::ast::expr::ReturnExpr) or | ||
/// [`QuestionMarkExpr`](marker_api::ast::expr::QuestionMarkExpr). | ||
/// | ||
/// This function is useful, for lints which suggest moving code snippets into | ||
/// a closure or different function. Return statements might prevent the suggested | ||
/// refactoring. | ||
pub fn contains_return<'ast>(cx: &'ast AstContext<'ast>, node: impl Traverseable<'ast, bool>) -> bool { | ||
for_each_expr(cx, node, |expr| { | ||
if matches!(expr, ExprKind::Return(_) | ExprKind::QuestionMark(_)) { | ||
ControlFlow::Break(true) | ||
} else { | ||
ControlFlow::Continue(()) | ||
} | ||
}) | ||
.is_some() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters