Skip to content

Commit

Permalink
add flint setup files
Browse files Browse the repository at this point in the history
  • Loading branch information
rempsyc committed Dec 16, 2024
1 parent c8a802d commit 13f0b74
Show file tree
Hide file tree
Showing 51 changed files with 2,072 additions and 0 deletions.
Binary file added flint/cache_file_state.rds
Binary file not shown.
49 changes: 49 additions & 0 deletions flint/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
keep:
- any_duplicated
- any_is_na
- class_equals
- condition_message
- double_assignment
- duplicate_argument
- empty_assignment
- equal_assignment
- equals_na
- expect_comparison
- expect_identical
- expect_length
- expect_named
- expect_not
- expect_null
- expect_true_false
- expect_type
- for_loop_index
- function_return
- implicit_assignment
- is_numeric
- length_levels
- length_test
- lengths
- library_call
- literal_coercion
- matrix_apply
- missing_argument
- nested_ifelse
- numeric_leading_zero
- outer_negation
- package_hooks
- paste
- redundant_equals
- redundant_ifelse
- rep_len
- right_assignment
- sample_int
- semicolon
- seq
- sort
- T_and_F_symbol
- todo_comment
- undesirable_function
- undesirable_operator
- unnecessary_nesting
- unreachable_code
- which_grepl
97 changes: 97 additions & 0 deletions flint/rules/builtin/T_and_F_symbol.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
id: true_false_symbol
language: r
severity: warning
rule:
pattern: T
kind: identifier
not:
any:
- precedes:
any:
- pattern: <-
- pattern: =
- regex: ^~$
- follows:
any:
- pattern: $
- regex: ^~$
- inside:
any:
- kind: parameter
- kind: call
- kind: binary_operator
follows:
regex: ^~$
stopBy: end
stopBy:
kind:
argument
fix: TRUE
message: Use TRUE instead of the symbol T.

---

id: true_false_symbol-2
language: r
severity: warning
rule:
pattern: F
kind: identifier
not:
any:
- precedes:
any:
- pattern: <-
- pattern: =
- regex: ^~$
- follows:
any:
- pattern: $
- regex: ^~$
- inside:
any:
- kind: parameter
- kind: call
- kind: binary_operator
follows:
regex: ^~$
stopBy: end
stopBy:
kind:
argument
fix: FALSE
message: Use FALSE instead of the symbol F.

---

id: true_false_symbol-3
language: r
severity: warning
rule:
pattern: T
kind: identifier
precedes:
any:
- pattern: <-
- pattern: =
not:
inside:
kind: argument
message: Don't use T as a variable name, as it can break code relying on T being TRUE.

---

id: true_false_symbol-4
language: r
severity: warning
rule:
pattern: F
kind: identifier
precedes:
any:
- pattern: <-
- pattern: =
not:
inside:
kind: argument
message: Don't use F as a variable name, as it can break code relying on F being FALSE.
13 changes: 13 additions & 0 deletions flint/rules/builtin/absolute_path.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# id: absolute_path-1
# language: r
# severity: warning
# rule:
# kind: string_content
# any:
# - regex: '^~[[:alpha:]]'
# - regex: '^~/[[:alpha:]]'
# - regex: '^[[:alpha:]]:'
# - regex: '^(/|~)$'
# - regex: '^/[[:alpha:]]'
# - regex: '^\\'
# message: Do not use absolute paths.
91 changes: 91 additions & 0 deletions flint/rules/builtin/any_duplicated.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
id: any_duplicated-1
language: r
severity: warning
rule:
pattern: any($$$ duplicated($MYVAR) $$$)
fix: anyDuplicated(~~MYVAR~~) > 0
message: anyDuplicated(x, ...) > 0 is better than any(duplicated(x), ...).

---

id: any_duplicated-2
language: r
severity: warning
rule:
any:
- pattern: length(unique($MYVAR)) == length($MYVAR)
- pattern: length($MYVAR) == length(unique($MYVAR))
fix: anyDuplicated(~~MYVAR~~) == 0L
message: anyDuplicated(x) == 0L is better than length(unique(x)) == length(x).

---

id: any_duplicated-3
language: r
severity: warning
rule:
pattern: length(unique($MYVAR)) != length($MYVAR)
fix: anyDuplicated(~~MYVAR~~) != 0L
message: |
Use anyDuplicated(x) != 0L (or > or <) instead of length(unique(x)) != length(x)
(or > or <).
---

id: any_duplicated-4
language: r
severity: warning
rule:
any:
- pattern: nrow($DATA) != length(unique($DATA$µCOL))
- pattern: length(unique($DATA$µCOL)) != nrow($DATA)
fix: anyDuplicated(~~DATA~~$~~COL~~) != 0L
message: |
anyDuplicated(DF$col) != 0L is better than length(unique(DF$col)) != nrow(DF)
---

# id: any_duplicated-5
# language: r
# severity: warning
# rule:
# any:
# - pattern:
# context: nrow($DATA) != length(unique($DATA[["µCOL"]]))
# strictness: ast
# - pattern:
# context: length(unique($DATA[["µCOL"]])) != nrow($DATA)
# strictness: ast
# fix: anyDuplicated(~~DATA~~[["~~COL~~"]]) != 0L
# message: |
# anyDuplicated(DF[["col"]]) != 0L is better than length(unique(DF[["col"]])) != nrow(DF)
#
# ---

id: any_duplicated-6
language: r
severity: warning
rule:
any:
- pattern: nrow($DATA) == length(unique($DATA$µCOL))
- pattern: length(unique($DATA$µCOL)) == nrow($DATA)
fix: anyDuplicated(~~DATA~~$~~COL~~) == 0L
message: |
anyDuplicated(DF$col) == 0L is better than length(unique(DF$col)) == nrow(DF)
# ---
#
# id: any_duplicated-7
# language: r
# severity: warning
# rule:
# any:
# - pattern:
# context: nrow($DATA) == length(unique($DATA[["µCOL"]]))
# strictness: ast
# - pattern:
# context: length(unique($DATA[["µCOL"]])) == nrow($DATA)
# strictness: ast
# fix: anyDuplicated(~~DATA~~[["~~COL~~"]]) == 0L
# message: |
# anyDuplicated(DF[["col"]]) == 0L is better than length(unique(DF[["col"]])) == nrow(DF)
7 changes: 7 additions & 0 deletions flint/rules/builtin/any_is_na.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
id: any_na-1
language: r
severity: warning
rule:
pattern: any($$$ is.na($MYVAR) $$$)
fix: anyNA(~~MYVAR~~)
message: anyNA(x) is better than any(is.na(x)).
42 changes: 42 additions & 0 deletions flint/rules/builtin/class_equals.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
id: class_equals-1
language: r
severity: warning
rule:
any:
- pattern: class($VAR) == $CLASSNAME
- pattern: $CLASSNAME == class($VAR)
not:
inside:
kind: argument
fix: inherits(~~VAR~~, ~~CLASSNAME~~)
message: Instead of comparing class(x) with ==, use inherits(x, 'class-name') or is.<class> or is(x, 'class')

---

id: class_equals-2
language: r
severity: warning
rule:
any:
- pattern: class($VAR) != $CLASSNAME
- pattern: $CLASSNAME != class($VAR)
not:
inside:
kind: argument
fix: "!inherits(~~VAR~~, ~~CLASSNAME~~)"
message: "Instead of comparing class(x) with !=, use !inherits(x, 'class-name') or is.<class> or is(x, 'class')"

---

id: class_equals-3
language: r
severity: warning
rule:
any:
- pattern: $CLASSNAME %in% class($VAR)
- pattern: class($VAR) %in% $CLASSNAME
constraints:
CLASSNAME:
kind: string
fix: inherits(~~VAR~~, ~~CLASSNAME~~)
message: Instead of comparing class(x) with %in%, use inherits(x, 'class-name') or is.<class> or is(x, 'class')
23 changes: 23 additions & 0 deletions flint/rules/builtin/condition_message.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
id: condition_message-1
language: r
severity: warning
rule:
pattern: $FUN($$$ paste0($$$MSG) $$$)
kind: call
not:
any:
- has:
kind: extract_operator
- has:
stopBy: end
kind: argument
has:
field: name
regex: "^collapse|recycle0$"
stopBy: end
constraints:
FUN:
regex: "^(packageStartupMessage|stop|warning)$"
fix: ~~FUN~~(~~MSG~~)
message: |
~~FUN~~(paste0(...)) can be rewritten as ~~FUN~~(...).
23 changes: 23 additions & 0 deletions flint/rules/builtin/double_assignment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
id: right_double_assignment
language: r
severity: hint
rule:
pattern: $RHS ->> $LHS
has:
field: rhs
kind: identifier
message: ->> can have hard-to-predict behavior; prefer assigning to a
specific environment instead (with assign() or <-).

---

id: left_double_assignment
language: r
severity: hint
rule:
pattern: $LHS <<- $RHS
has:
field: lhs
kind: identifier
message: <<- can have hard-to-predict behavior; prefer assigning to a
specific environment instead (with assign() or <-).
46 changes: 46 additions & 0 deletions flint/rules/builtin/duplicate_argument.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
id: duplicate_argument-1
language: r
severity: warning
rule:
# Look for a function argument...
kind: argument
any:
- has:
kind: identifier
field: name
pattern: $OBJ
- has:
kind: string_content
pattern: $OBJ
stopBy: end

# ... that follows other argument(s) with the same name...
follows:
kind: argument
stopBy: end
has:
stopBy: end
kind: identifier
field: name
pattern: $OBJ

# ... inside a function call (or a subset environment for data.table)...
inside:
kind: arguments
follows:
any:
- kind: identifier
pattern: $FUN
- kind: string
inside:
any:
- kind: call
- kind: subset

# ... that is not a function listed below.
constraints:
FUN:
not:
regex: ^(mutate|transmute)$

message: Avoid duplicate arguments in function calls.
Loading

0 comments on commit 13f0b74

Please sign in to comment.