Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-unused-new: should not be triggered #1839

Closed
eamodio opened this issue Dec 10, 2016 · 1 comment · Fixed by #2269 or singapore/lint-condo#263
Closed

no-unused-new: should not be triggered #1839

eamodio opened this issue Dec 10, 2016 · 1 comment · Fixed by #2269 or singapore/lint-condo#263

Comments

@eamodio
Copy link

eamodio commented Dec 10, 2016

Bug Report

  • TSLint version: 4.0.2
  • TypeScript version: 2.1.4
  • Running TSLint via: VSCode

TypeScript code being linted

class Foo {
    value: string;
 }
class Bar {
    foo: Foo;
 }

function setFoo(bar: Bar) {
    bar.foo || (bar.foo = new Foo()); // lint error here (no-unused-new)
    bar.foo.value = 'hi';
}

let bar = new Bar();
setFoo(bar);

with tslint.json configuration:

{
    "rules": {
        "arrow-parens": false,
        "class-name": true,
        "comment-format": [
            false
        ],
        "curly": false,
        "indent": [
            true,
            "spaces"
        ],
        "new-parens": true,
        "no-duplicate-variable": true,
        "no-eval": true,
        "no-for-in-array": false,
        "no-internal-module": true,
        "no-reference": true,
        "no-trailing-whitespace": true,
        "no-unsafe-finally": true,
        "no-unused-expression": false,
        "no-unused-new": true,
        "no-var-keyword": true,
        "no-var-requires": true,
        "object-literal-key-quotes": [
            true,
            "as-needed"
        ],
        "one-line": [
            true,
            "check-open-brace",
            "check-whitespace"
        ],
        "one-variable-per-declaration": [
            true,
            "ignore-for-loop"
        ],
        "ordered-imports": [
            false,
            {
                "named-imports-order": "case-insensitive"
            }
        ],
        "quotemark": [
            true,
            "single",
            "avoid-escape"
        ],
        "radix": true,
        "semicolon": [
            true,
            "always"
        ],
        "trailing-comma": [
            true,
            {
                "multiline": "never",
                "singleline": "never"
            }
        ],
        "triple-equals": [
            true,
            "allow-null-check"
        ],
        "typedef-whitespace": [
            true,
            {
                "call-signature": "nospace",
                "index-signature": "nospace",
                "parameter": "nospace",
                "property-declaration": "nospace",
                "variable-declaration": "nospace"
            }
        ],
        "use-isnan": true,
        "variable-name": [
            true,
            "allow-leading-underscore",
            "allow-pascal-case",
            "ban-keywords",
            "check-format"
        ],
        "whitespace": [
            true,
            "check-branch",
            "check-decl",
            "check-operator",
            "check-module",
            "check-separator",
            "check-type"
        ]
    }
}

Actual behavior

[tslint] do not use 'new' for side effects (no-unused-new)

Expected behavior

No lint error

@andy-hanson
Copy link
Contributor

The implementation of no-unused-new seems overly complicated. It looks inside of an expression for a new subexpression. This apparently catches 5 + new Foo(), but that's not really useful. The type checker will catch that and so will no-unused-expression. Why not deprecate no-unused-new and make it an option of no-unused-expression?

nchen63 pushed a commit that referenced this issue Mar 10, 2017
Integrate `no-unused-new` into this rule.
[new-rule-option] `allow-new-side-effects` restores the old behaviour to allow `new` for side effects
[bugfix] allow comma separated assignments
Fixes: #2058
[bugfix] allow indirect eval: `(0, eval)("");`
[enhancement] checking for unused new can now use option `allow-fast-null-checks`
Fixes: #1839
[enhancement] find unused comma separated expressions in all locations of the code
[enhancement] find unused expressions inside void expression
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants