# we don't fix the string comparison cases like `x != ""` with `nzchar(x)`
# since it will fail for factors (string comparison coerces whereas `nzchar()` errors)
# <https://github.com/etiennebacher/flir/pull/102#discussion_r2328611044>
#
# `nzchar(x, keepNA = TRUE)` and `nchar(x, keepNA = TRUE) > 0` are equivalent
# `nzchar(x, keepNA = NA)` and `nchar(x, keepNA = NA) > 0` are not equivalent
# `nchar(x, type = "width")` also not strictly compatible with `nzchar(x)`

id: nzchar-1
language: r
severity: warning
rule:
  any:
    - pattern: $X > ""
    - pattern: $X > ''
    - pattern: '"" < $X'
    - pattern: "'' < $X"
message: Use nzchar(x) instead of x > "".
  Use nzchar(x, keepNA = TRUE) if missing data is possible.
  Use nzchar(as.character(x)) if x may not be a character vector.
---

id: nzchar-2
language: r
severity: warning
rule:
  any:
    - pattern: $X != ""
    - pattern: $X != ''
    - pattern: '"" != $X'
    - pattern: "'' != $X"
  not:
    inside:
      any:
        - kind: if_statement
        - kind: while_statement
      field: condition
      stopBy:
        kind: call
message: Use nzchar(x) instead of x != "".
  Use nzchar(x, keepNA = TRUE) if missing data is possible.
  Use nzchar(as.character(x)) if x may not be a character vector.
---

id: nzchar-3
language: r
severity: warning
rule:
  any:
    - pattern: $X <= ""
    - pattern: $X <= ''
    - pattern: '"" >= $X'
    - pattern: "'' >= $X"
message: Use !nzchar(x) instead of x <= "".
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.
  Use !nzchar(as.character(x)) if x may not be a character vector.
---

id: nzchar-4
language: r
severity: warning
rule:
  any:
    - pattern: $X == ""
    - pattern: $X == ''
    - pattern: '"" == $X'
    - pattern: "'' == $X"
  not:
    inside:
      any:
        - kind: if_statement
        - kind: while_statement
      field: condition
      stopBy:
        kind: call
message: Use !nzchar(x) instead of x == "".
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.
  Use !nzchar(as.character(x)) if x may not be a character vector.
---

id: nzchar-5
language: r
severity: warning
rule:
  any:
    - pattern: $X >= ""
    - pattern: $X >= ''
    - pattern: '"" <= $X'
    - pattern: "'' <= $X"
message: x >= "" is always true, maybe you want nzchar(x)?
  Use nzchar(x, keepNA = TRUE) if missing data is possible.
  Use nzchar(as.character(x)) if x may not be a character vector.

---

id: nzchar-6
language: r
severity: warning
rule:
  any:
    - pattern: $X < ""
    - pattern: $X < ''
    - pattern: '"" > $X'
    - pattern: "'' > $X"
message: x < "" is always false, maybe you want !nzchar(x)?
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.
  Use !nzchar(as.character(x)) if x may not be a character vector.

---

id: nzchar-7
language: r
severity: warning
rule:
  any:
    - pattern: nchar($X) > $ZERO
    - pattern: $ZERO < nchar($X)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
fix: nzchar(~~X~~, keepNA = TRUE)
message: Use nzchar(x) instead of nchar(x) > 0.
  Use nzchar(x, keepNA = TRUE) if missing data is possible.

---

id: nzchar-7b
language: r
severity: warning
rule:
  any:
    - pattern: nchar($X, keepNA = $KNA) > $ZERO
    - pattern: $ZERO < nchar($X, keepNA = $KNA)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
message: Use nzchar(x) instead of nchar(x) > 0.
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.

---

id: nzchar-8
language: r
severity: warning
rule:
  any:
    - pattern: nchar($X) != $ZERO
    - pattern: $ZERO != nchar($X)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
fix: nzchar(~~X~~, keepNA = TRUE)
message: Use nzchar(x) instead of nchar(x) != 0.
  Use nzchar(x, keepNA = TRUE) if missing data is possible.

---

# only lint since `nzchar()` and `nchar()` don't treat `keepNA = NA` consistently
id: nzchar-8b
language: r
severity: warning
rule:
  any:
    - pattern: nchar($X, keepNA = $KNA) != $ZERO
    - pattern: $ZERO != nchar($X, keepNA = $KNA)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
message: Use nzchar(x) instead of nchar(x) != 0.
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.

---

id: nzchar-9
language: r
severity: warning
rule:
  any:
    - pattern: nchar($X) <= $ZERO
    - pattern: $ZERO >= nchar($X)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
fix: "!nzchar(~~X~~, keepNA = TRUE)"
message: Use !nzchar(x) instead of nchar(x) <= 0.
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.

---

id: nzchar-9b
language: r
severity: warning
rule:
  any:
    - pattern: nchar($X, keepNA = $KNA) <= $ZERO
    - pattern: $ZERO <= nchar($X, keepNA = $KNA)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
message: Use !nzchar(x) instead of nchar(x) <= 0.
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.

---

id: nzchar-10
language: r
severity: warning
rule:
  any:
    - pattern: nchar($X) == $ZERO
    - pattern: $ZERO == nchar($X)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
fix: "!nzchar(~~X~~, keepNA = TRUE)"
message: Use !nzchar(x) instead of nchar(x) == 0.
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.

---

id: nzchar-10b
language: r
severity: warning
rule:
  any:
    - pattern: nchar($X, keepNA = $KNA) == $ZERO
    - pattern: $ZERO == nchar($X, keepNA = $KNA)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
message: Use !nzchar(x) instead of nchar(x) == 0.
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.

---

id: nzchar-11
language: r
severity: warning
rule:
  any:
    - pattern: nchar($$$X) >= $ZERO
    - pattern: $ZERO <= nchar($$$X)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
message: nchar(x) >= 0 is always true, maybe you want nzchar(x)?
  Use nzchar(x, keepNA = TRUE) if missing data is possible.

---

id: nzchar-12
language: r
severity: warning
rule:
  any:
    - pattern: nchar($$$X) < $ZERO
    - pattern: $ZERO > nchar($$$X)
constraints:
  ZERO:
    regex: ^0?+\.0*$|^0+L?$
message: nchar(x) < 0 is always false, maybe you want !nzchar(x)?
  Use !nzchar(x, keepNA = TRUE) if missing data is possible.
