Skip to content

lint useless loop

github-actions[bot] edited this page Aug 24, 2025 · 1 revision

This document was generated from 'src/documentation/print-linter-wiki.ts' on 2025-08-24, 20:03:27 UTC presenting an overview of flowR's linter (v2.4.7, using R v4.5.0). Please do not edit this file/wiki page directly.

Useless Loops [overview]

smell readability

This rule is a best-effort rule.

Detect loops which only iterate once
This linting rule is implemented in src/linter/rules/useless-loop.ts.

Configuration

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

Examples

for(i in c(1)) { print(i) }

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

[ { "type": "linter",   "rules": [ { "name": "useless-loop",     "config": {} } ] } ]

Results (prettified and summarized):

Query: linter (0 ms)
   ╰ Useless Loops (useless-loop):
       ╰ certain:
           ╰ for-loop at 1.1-27 only loops once
       ╰ Metadata: {"numOfUselessLoops":1,"searchTimeMs":0,"processTimeMs":0}
All queries together required ≈0 ms (1ms accuracy, total 9 ms)

Show Detailed Results as Json

The analysis required 9.3 ms (including parsing and normalization and the query) within the generation environment.

In general, the JSON contains the Ids of the nodes in question as they are present in the normalized AST or the dataflow graph of flowR. Please consult the Interface wiki page for more information on how to get those.

{
  "linter": {
    "results": {
      "useless-loop": {
        "results": [
          {
            "certainty": "certain",
            "name": "for",
            "range": [
              1,
              1,
              1,
              27
            ]
          }
        ],
        ".meta": {
          "numOfUselessLoops": 1,
          "searchTimeMs": 0,
          "processTimeMs": 0
        }
      }
    },
    ".meta": {
      "timing": 0
    }
  },
  ".meta": {
    "timing": 0
  }
}

Additional Examples

These examples are synthesized from the test cases in: test/functionality/linter/lint-useless-loop.test.ts

Test Case: i in c(1)

Given a for-loop the linter checks, if the vector only contains one element

Given the following input:

for(i in c(1)) { print(i) }

And using the following configuration:

undefined

We expect the linter to report the following:

certainty: LintingResultCertainty.Certain,
name:      'for',
range:     [1,1,1,27]

See here for the test-case implementation.

Test Case: always break

Given a loop the linter checks, if the loop is always stopped after the first iteration

Given the following input:

for(i in c(1,2,3)) { print(i); break }

And using the following configuration:

undefined

We expect the linter to report the following:

certainty: LintingResultCertainty.Certain,
name:      'for',
range:     [1,1,1,38]

See here for the test-case implementation.

Clone this wiki locally