question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-unused-vars false positive for variable declared in one scope but only used in some subscope(s)

See original GitHub issue

*OSX

  • **ESLint Version: ** 4.19.1
  • **Node Version: ** 8.9.4
  • **npm Version: ** 5.6.0

What parser (default, Babel-ESLint, etc.) are you using? Default

Please show your full configuration:

Configuration
module.exports = {
    root: true,
    parserOptions: {
        ecmaVersion: 2017,
        sourceType: 'module'
    },
    extends: 'eslint:recommended',
    env: {
        'node': true,
        'es6': true
    },
    rules: {
        // Additional rules
        //  http://eslint.org/docs/rules

        // ESLint Possible Errors
        "no-template-curly-in-string": "error",
        "valid-jsdoc": [ "error", {
            "prefer": { "arg": "param", "argument": "param", "class": "constructor", "return": "returns", "virtual": "abstract" },
            "preferType": { "boolean": "Boolean", "number": "Number", "object": "Object", "string": "String" },
        }],

        // ESLint best practices
        "block-scoped-var": "error",
        "consistent-return": "error",
        "default-case": "error",
        "guard-for-in": "error",
        "no-caller": "error",
        "no-div-regex": "error",
        "no-eval": "error",
        "no-empty-function": "error",
        "no-eq-null": "error",
        "no-extend-native": "error",
        "no-floating-decimal": "error",
        "no-implicit-coercion": "error",
        // "no-invalid-this": "error",
        "no-iterator": "error",
        "no-labels": "error",
        "no-loop-func": "error",
        "no-multi-str": "error",
        "no-new": "error",
        "no-new-func": "error",
        "no-new-wrappers": "error",
        "no-octal-escape": "error",
        "no-param-reassign": "error",
        "no-proto": "error",
        "no-return-assign": "error",
        "no-return-await": "error",
        "no-script-url": "error",
        "no-self-compare": "error",
        "no-sequences": "error",
        "no-throw-literal": "error",
        "no-unmodified-loop-condition": "error",
        "no-unused-expressions": "error",
        "no-useless-call": "error",
        "no-useless-concat": "error",
        "no-useless-escape": "error",
        "no-void": "error",
        "no-warning-comments": [ "warn", { "terms": ["todo", "fixme"], "location": "anywhere" } ],
        "no-with": "error",
        "prefer-promise-reject-errors": "error",
        "radix": "error",
        "require-await": "error",
        "wrap-iife": "error",

        // ESLint Strict Mode rules
        "strict": "error",

        // ESLint Variables rules
        "init-declarations": "error",
        "no-catch-shadow": "error",
        "no-label-var": "error",
        "no-restricted-globals": "error",
        "no-shadow": "error",
        "no-shadow-restricted-names": "error",
        "no-undef-init": "error",
        "no-undefined": "error",
        "no-use-before-define": "error",

        // ESLint Stylistic Issues
        "indent": "error",
        "keyword-spacing": "error",
        "no-tabs": "error",
        "semi": "error",
        "sort-imports": [ "error", { "ignoreCase": true }],
        "space-unary-ops": "error",

        // ECMAScript 6
        "arrow-parens": "error",
        "no-confusing-arrow": "error",
        "no-var": "error",
        "prefer-const": "warn",
        "prefer-numeric-literals": "error",
        "prefer-rest-params": "error",
        "prefer-spread": "error",
        "prefer-template": "error",

        // Project specific
        "no-console": 0
    }
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

const generateBinaryHeader = (uuidStr, {num1, num2, num3}, someObj) => {
    const header = Buffer.alloc(30);
    let i = 0;
    {
        const begin = Buffer.from([0xAA, 0x00, 0x00, 0x88, 0x00, 0x00]); // Magic number
        i = i + begin.copy(header, i);
    }

    {
        const uuidBuf = uuidStr2Buf(uuidStr);
        i = i + uuidBuf.copy(header, i);
    }

    {
        const versionBuf = Buffer.from([num1, num2, num3, 0x00, someObj.num, 0x00 ,0xEF, 0xEF]);
        i = i + versionBuf.copy(header, i);
    }

    return header;
};
npm start
// package.json
// ...
  "scripts": {
    "start": "eslint .; ./my_start_script",
  },
// ...

What did you expect to happen? Variable i is used in the sub scope. Sub-scope defined for code clarity. This example is simple, but elsewhere in our code things are more complex and the above pattern still exists. The variable is both read and written to, therefore it should be considered used. What actually happened? Please include the actual, raw output from ESLint.

> eslint .; ./tfp_pkg


/Users/me/projects/project_name/src/code.js
  192:9   error  'i' is assigned a value but never used  no-unused-vars
  253:17  error  'i' is assigned a value but never used  no-unused-vars

✖ 2 problems (2 errors, 0 warnings)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
not-an-aardvarkcommented, Apr 13, 2018

In the function call, you are using the handler variable declared here:

btn.addEventListener('click', (event, handler) => subscribe(event, handler));
                                      ^^^^^^^

However, you are not using the handler variable declared here:

const handler = 'x';
      ^^^^^^^

You might have intended to make your code look like this instead:

const handler = 'x';
btn.addEventListener('click', (event) => subscribe(event, handler));

function subscribe(event, handler) {
 ...
}
1reaction
not-an-aardvarkcommented, Apr 12, 2018

Hi, thanks for the report. However, this is working as intended.

@TuckerDowns In your example, the last assignment to i is unused, because i is never used after that. You could just replace that line with versionBuf.copy(header, i);.

@vitaly-zdanevich Your example is working as intended because the handler variable is never actually used. You are creating two functions that shadow the handler variable, but if you use handler inside those functions it will refer to the shadowed variable and not the variable that you declared at the top of your file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript-eslint/no-unused-vars false positive in type ...
And we have @typescript-eslint/no-unused-vars error in string with type declaration, which says 'name' is defined but never used. example of ...
Read more >
no-unused-vars - ESLint - Pluggable JavaScript Linter
This rule is aimed at eliminating unused variables, functions, and function parameters. A variable foo is considered to be used if any of...
Read more >
C-STAT® Static Analysis Guide - IAR Systems
The software described in this document is furnished under a license and may only be used or copied in accordance with the terms...
Read more >
Chapter - Pearsoncmg.com
This code is noncompliant because, even though variable i is not intention- ally used outside the for loop, it is declared in method...
Read more >
Donate - Eclipse Git repositories
NotAConstant); // do not report fake used variable if (local.useFlag == LocalVariableBinding.UNUSED && (local.declaration != null) // unused (and non ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found