False Positive for typescript:S6440: React Hook may be executed more than once

  • Language: Typescript React
  • Rule #: S6440
  • False Positive Reason: The hook is being called at the top of the component/hook, not nested in any loop or condition.
  • Version: Data Center Edition v2025.1.2 (108896)

There is a for loop in the code, but the hook itself isn’t being called in the for loop.

Example of code flagged as a bug

// this flags typescript s6440
const useFileData = () => {
    const { filePrefix } = useFileContext()

    const files: Array<string> = []
    for (let i = 2; i <= 5; i++) {
        files.push(filePrefix + `${i}.json`)
    }

    // ...rest of logic

}

Example of equivalent code that isn’t considered a bug

const useFileData = () => {
    const { filePrefix } = useFileContext()

    const files = Array.from({ length: 4 }, (_, i) => filePrefix + `${i + 2}.json`)

    // ...rest of logic

}

I already have eslint which is capable of warning for the rules of hooks, and it doesn’t flag this code.

Hi @yungwaj,

Seems like your snippet is recognized as a React component as well as being recognized by this rule - Rules of Hooks – React - as Sonar, we are merely borrowing this rule.

Therefore, I guess it must be a false positive of the eslint plugin that we are invoking.

Also, I wasn’t able to repro. Would you be able to setup a sample repo show-casing this issue?

Kind regards,
Michal

1 Like