This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: #200 from feature/184-ignore-rule-comments-staging
184 - Ignore rule comments
- Loading branch information
Showing
12 changed files
with
305 additions
and
12 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
127 changes: 127 additions & 0 deletions
127
toolchains/solidity/core/crates/linter-lib/src/types/ignore.rs
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,127 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
macro_rules! define_ignore_enum { | ||
($name:ident, $($variant:ident => $str:expr),* $(,)?) => { | ||
#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Debug)] | ||
pub enum $name { | ||
$($variant),* | ||
} | ||
|
||
impl ToString for $name { | ||
fn to_string(&self) -> String { | ||
match self { | ||
$(Self::$variant => $str),* | ||
} | ||
.to_string() | ||
} | ||
} | ||
|
||
impl $name { | ||
pub fn iter() -> impl Iterator<Item = Self> { | ||
[$(Self::$variant),*].iter().copied() | ||
} | ||
} | ||
}; | ||
} | ||
|
||
define_ignore_enum! { | ||
Ignore, | ||
NextLine => "solidhunter-disable-next-line", | ||
SameLine => "solidhunter-disable-line", | ||
Disable => "solidhunter-disable", | ||
Enable => "solidhunter-enable", | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct IgnoreComment { | ||
pub line_number: usize, | ||
pub ignore_type: Ignore, | ||
pub rule_ids: Option<Vec<String>>, | ||
} | ||
|
||
impl IgnoreComment { | ||
pub fn from_line(line_number: usize, line: &str) -> Option<Self> { | ||
for ignore in Ignore::iter() { | ||
let ignore_str = ignore.to_string(); | ||
if line.contains(&ignore_str) { | ||
let rule_ids_str = line.split(&ignore_str).nth(1); | ||
let rule_ids = rule_ids_str | ||
.map(|s| { | ||
s.split(' ') | ||
.map(|s| s.trim()) | ||
.filter(|s| !s.is_empty()) | ||
.map(|s| s.to_string()) | ||
.collect::<Vec<String>>() | ||
}) | ||
.filter(|s| !s.is_empty()); | ||
return Some(Self { | ||
line_number, | ||
ignore_type: ignore, | ||
rule_ids, | ||
}); | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct DisableRange { | ||
pub start_line: usize, | ||
pub end_line: usize, | ||
pub ignore_type: Ignore, | ||
pub rule_ids: Vec<String>, | ||
} | ||
|
||
pub fn build_disable_ranges(comments: Vec<IgnoreComment>) -> Vec<DisableRange> { | ||
let mut disable_ranges = vec![]; | ||
let mut current_range: Option<DisableRange> = None; | ||
|
||
for comment in comments { | ||
let line_number = comment.line_number; | ||
let ignore_type = comment.ignore_type; | ||
let rule_ids = comment.rule_ids.unwrap_or(vec![]); | ||
|
||
match ignore_type { | ||
Ignore::Enable | Ignore::Disable => { | ||
if let Some(range) = current_range { | ||
disable_ranges.push(range); | ||
} | ||
current_range = Some(DisableRange { | ||
start_line: line_number, | ||
end_line: line_number, | ||
ignore_type, | ||
rule_ids, | ||
}); | ||
} | ||
Ignore::SameLine => { | ||
if let Some(range) = &mut current_range { | ||
range.end_line = line_number; | ||
} | ||
disable_ranges.push(DisableRange { | ||
start_line: line_number, | ||
end_line: line_number, | ||
ignore_type, | ||
rule_ids, | ||
}); | ||
} | ||
Ignore::NextLine => { | ||
if let Some(range) = &mut current_range { | ||
range.end_line = line_number; | ||
} | ||
disable_ranges.push(DisableRange { | ||
start_line: line_number + 1, | ||
end_line: line_number + 1, | ||
ignore_type, | ||
rule_ids, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
if let Some(range) = current_range { | ||
disable_ranges.push(range); | ||
} | ||
|
||
disable_ranges | ||
} |
8 changes: 8 additions & 0 deletions
8
toolchains/solidity/core/crates/linter-lib/testdata/SolidHunterIgnoreRule/.solidhunter.json
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,8 @@ | ||
{ | ||
"name": "solidhunter", "rules": [ | ||
{ | ||
"id": "avoid-tx-origin", | ||
"severity": "WARNING" | ||
} | ||
] | ||
} |
66 changes: 66 additions & 0 deletions
66
toolchains/solidity/core/crates/linter-lib/testdata/SolidHunterIgnoreRule/file.sol
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,66 @@ | ||
pragma solidity 0.8.0; | ||
|
||
contract Test { | ||
function awesome() public returns (address) { | ||
// solidhunter-disable-next-line exist-with-error | ||
return tx.origin; // solidhunter-disable-line exist-with-error | ||
} | ||
|
||
function awesomeLineUp() public returns (address) { | ||
// solidhunter-disable-next-line avoid-tx-origin | ||
return tx.origin; | ||
} | ||
function awesomeSameLine() public returns (address) { | ||
return tx.origin; // solidhunter-disable-line avoid-tx-origin | ||
} | ||
|
||
function notAwesome() public returns (address) { | ||
// solidhunter-disable-next-line not-exist-no-error | ||
return msg.sender; // solidhunter-disable-line not-exist-no-error | ||
} | ||
|
||
function awesomeLineUpAny() public returns (address) { | ||
// solidhunter-disable-next-line | ||
return tx.origin; | ||
} | ||
function awesomeSameLineAny() public returns (address) { | ||
return tx.origin; // solidhunter-disable-line | ||
} | ||
|
||
function awesomeLineUpAny() public returns (address) { | ||
// solidhunter-disable-next-line dummy-rule avoid-tx-origin dummy-rule2 | ||
return tx.origin; | ||
} | ||
function awesomeSameLineAny() public returns (address) { | ||
return tx.origin; // solidhunter-disable-line dummy-rule avoid-tx-origin dummy-rule2 | ||
} | ||
|
||
// solidhunter-disable | ||
function awesome() public returns (address) { | ||
return tx.origin; | ||
} | ||
// solidhunter-enable | ||
|
||
|
||
// solidhunter-disable | ||
// solidhunter-disable | ||
// solidhunter-enable | ||
function awesome() public returns (address) { | ||
return tx.origin; | ||
} | ||
// solidhunter-enable | ||
|
||
// solidhunter-disable avoid-tx-origin | ||
function awesome() public returns (address) { | ||
return tx.origin; | ||
} | ||
// solidhunter-enable avoid-tx-origin | ||
|
||
// solidhunter-disable avoid-tx-origin | ||
// solidhunter-disable | ||
// solidhunter-enable avoid-tx-origin | ||
function awesome() public returns (address) { | ||
return tx.origin; | ||
} | ||
// solidhunter-enable | ||
} |
1 change: 1 addition & 0 deletions
1
toolchains/solidity/core/crates/linter-lib/testdata/SolidHunterIgnoreRule/findings.csv
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 @@ | ||
avoid-tx-origin:6:15:6:24 |
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
Oops, something went wrong.