Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
id: is-err-then-unwrap-err
snapshots:
? |
if result.is_err() {
report(result.unwrap_err());
}
: labels:
- source: |-
if result.is_err() {
report(result.unwrap_err());
}
style: primary
start: 0
end: 55
- source: result.is_err()
style: secondary
start: 3
end: 18
- source: result.unwrap_err()
style: secondary
start: 32
end: 51
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
id: is-ok-then-unwrap
snapshots:
? |
if result.is_ok() {
consume(result.unwrap());
}
: labels:
- source: |-
if result.is_ok() {
consume(result.unwrap());
}
style: primary
start: 0
end: 51
- source: result.is_ok()
style: secondary
start: 3
end: 17
- source: result.unwrap()
style: secondary
start: 32
end: 47
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
id: is-some-then-unwrap
snapshots:
? |
if value.is_some() {
consume(value.unwrap());
}
: labels:
- source: |-
if value.is_some() {
consume(value.unwrap());
}
style: primary
start: 0
end: 51
- source: value.is_some()
style: secondary
start: 3
end: 18
- source: value.unwrap()
style: secondary
start: 33
end: 47
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id: prefer-less-than-comparisons
snapshots:
larger > smaller:
labels:
- source: larger > smaller
style: primary
start: 0
end: 16
larger >= smaller:
labels:
- source: larger >= smaller
style: primary
start: 0
end: 17
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
id: unwrap-in-production
snapshots:
? |
#[cfg(test)]
fn test_helper() {}

#[allow(dead_code)]
fn production() {
value.unwrap();
}
: labels:
- source: value.unwrap()
style: primary
start: 76
end: 90
value.unwrap():
labels:
- source: value.unwrap()
style: primary
start: 0
end: 14
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id: unwrap-or-default
snapshots:
value.unwrap_or(Default::default()):
labels:
- source: value.unwrap_or(Default::default())
style: primary
start: 0
end: 35
value.unwrap_or(String::default()):
labels:
- source: value.unwrap_or(String::default())
style: primary
start: 0
end: 34
15 changes: 15 additions & 0 deletions .config/ast-grep/rule-tests/is-err-then-unwrap-err-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
id: is-err-then-unwrap-err
valid:
- |
if let Err(error) = result {
report(error);
}
- |
if result.is_err() {
report(other.unwrap_err());
}
invalid:
- |
if result.is_err() {
report(result.unwrap_err());
}
15 changes: 15 additions & 0 deletions .config/ast-grep/rule-tests/is-ok-then-unwrap-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
id: is-ok-then-unwrap
valid:
- |
if let Ok(value) = result {
consume(value);
}
- |
if result.is_ok() {
consume(other.unwrap());
}
invalid:
- |
if result.is_ok() {
consume(result.unwrap());
}
15 changes: 15 additions & 0 deletions .config/ast-grep/rule-tests/is-some-then-unwrap-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
id: is-some-then-unwrap
valid:
- |
if let Some(value) = value {
consume(value);
}
- |
if value.is_some() {
consume(other.unwrap());
}
invalid:
- |
if value.is_some() {
consume(value.unwrap());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
id: prefer-less-than-comparisons
valid:
- smaller < larger
- smaller <= larger
- next_left() > next_right()
- object.left > object.right
invalid:
- larger > smaller
- larger >= smaller
60 changes: 60 additions & 0 deletions .config/ast-grep/rule-tests/unwrap-in-production-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
id: unwrap-in-production
valid:
- value?
- value.expect("value was validated above")
- |
#[cfg(test)]
mod tests {
#[test]
fn works() {
value.unwrap();
}
}
- |
#[cfg(test)]
fn test_helper() {
value.unwrap();
}
- |
#[test]
fn works() {
value.unwrap();
}
- |
#[test]
#[should_panic]
fn panics() {
value.unwrap();
}
- |
#[cfg(test)]
#[allow(dead_code)]
fn test_helper() {
value.unwrap();
}
- |
#[cfg(test)]
#[allow(dead_code)]
mod tests {
fn helper() {
value.unwrap();
}
}
- |
#[cfg(test)]
#[allow(dead_code)]
impl Fixture {
fn helper() {
value.unwrap();
}
}
invalid:
- value.unwrap()
- |
#[cfg(test)]
fn test_helper() {}

#[allow(dead_code)]
fn production() {
value.unwrap();
}
7 changes: 7 additions & 0 deletions .config/ast-grep/rule-tests/unwrap-or-default-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
id: unwrap-or-default
valid:
- value.unwrap_or_default()
- value.unwrap_or(fallback)
invalid:
- value.unwrap_or(Default::default())
- value.unwrap_or(String::default())
14 changes: 14 additions & 0 deletions .config/ast-grep/rules/is-err-then-unwrap-err.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id: is-err-then-unwrap-err
language: Rust
severity: warning
message: Replace an is_err() check followed by unwrap_err() with if let Err(...).
rule:
kind: if_expression
all:
- has:
field: condition
pattern: $VALUE.is_err()
- has:
field: consequence
stopBy: end
pattern: $VALUE.unwrap_err()
14 changes: 14 additions & 0 deletions .config/ast-grep/rules/is-ok-then-unwrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id: is-ok-then-unwrap
language: Rust
severity: warning
message: Replace an is_ok() check followed by unwrap() with if let Ok(...).
rule:
kind: if_expression
all:
- has:
field: condition
pattern: $VALUE.is_ok()
- has:
field: consequence
stopBy: end
pattern: $VALUE.unwrap()
14 changes: 14 additions & 0 deletions .config/ast-grep/rules/is-some-then-unwrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id: is-some-then-unwrap
language: Rust
severity: warning
message: Replace an is_some() check followed by unwrap() with if let Some(...).
rule:
kind: if_expression
all:
- has:
field: condition
pattern: $VALUE.is_some()
- has:
field: consequence
stopBy: end
pattern: $VALUE.unwrap()
17 changes: 17 additions & 0 deletions .config/ast-grep/rules/prefer-less-than-comparisons.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
id: prefer-less-than-comparisons
language: Rust
severity: hint
message: Put the lesser value on the left and use < or <= when comparing simple values.
rule:
any:
- pattern: $LEFT > $RIGHT
- pattern: $LEFT >= $RIGHT
constraints:
LEFT:
any:
- kind: identifier
- kind: integer_literal
RIGHT:
any:
- kind: identifier
- kind: integer_literal
40 changes: 40 additions & 0 deletions .config/ast-grep/rules/unwrap-in-production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
id: unwrap-in-production
language: Rust
severity: warning
message: Avoid unwrap() outside test code; propagate the error or document the invariant with expect().
ignores:
- "**/benches/**"
- "**/examples/**"
- "**/tests/**"
- "**/tests.rs"
- "**/*-tests.rs"
- "**/*_test.rs"
- "**/*_tests.rs"
- "**/test_*.rs"
rule:
all:
- pattern: $VALUE.unwrap()
- not:
inside:
stopBy: end
any:
- kind: mod_item
follows:
stopBy:
not:
kind: attribute_item
pattern: "#[cfg(test)]"
- kind: function_item
follows:
stopBy:
not:
kind: attribute_item
any:
- pattern: "#[cfg(test)]"
- pattern: "#[test]"
- kind: impl_item
follows:
stopBy:
not:
kind: attribute_item
pattern: "#[cfg(test)]"
8 changes: 8 additions & 0 deletions .config/ast-grep/rules/unwrap-or-default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
id: unwrap-or-default
language: Rust
severity: warning
message: Use unwrap_or_default() instead of constructing a default value eagerly.
rule:
any:
- pattern: $VALUE.unwrap_or(Default::default())
- pattern: $VALUE.unwrap_or($TYPE::default())
Loading
Loading