Skip to content

[Linting Rule] Class String Comparison

github-actions[bot] edited this page Oct 25, 2026 · 1 revision

Generated from 'wiki-linter.ts' on 2026-10-25, 09:29:25 UTC (v2.16.6), do not edit directly.

Class String Comparison [overview]

bug cmd-check 37 quickfix robustness

This rule ports the following steps of R CMD check (see the conformance table for the legend):

# Step Coverage Note
37 R code for possible problems (book) 🟩⭐ ⭐ also quick fixes, deprecated arguments, environments resolved through variables, hooks followed into callees

This rule is a best-effort rule.

Finds if/while conditions comparing class(x) with a string, which fail for objects with several classes. Offers inherits(x, ...) as a quick fix.
This linting rule is implemented in src/linter/rules/cmd-check/class-string-comparison.ts.

Configuration

Linting rules can be configured by passing a configuration object to the linter query as shown in the example below. The class-string-comparison rule accepts the following configuration options:

  • checkWhile
    also check the conditions of while loops, which R CMD check does not (default true)
    R CMD check #37
  • resolveStrings
    also flag a comparison with a variable flowR resolves to a string, which R CMD check cannot see (default true)
    R CMD check #37

Examples

f <- function(x) if(class(x) == "data.frame") nrow(x) else length(x)

The linting query can be used to run this rule on the above example:

[ { "type": "linter",   "rules": [ { "name": "class-string-comparison",     "config": {} } ] } ]

Results (prettified and summarized):

Query: linter (2 ms)
   ╰ Class String Comparison (class-string-comparison):
       ╰ certain:
           ╰ class() compared to "data.frame" at 1.21-44 (1 quick fix(es) available)

More examples are the test cases in test/functionality/linter/cmd-check/lint-class-string-comparison.test.ts.

Clone this wiki locally