Skip to content

Commit

Permalink
Unused type parameters should be checked by --noUnusedParameters, not (
Browse files Browse the repository at this point in the history
…#21167)

--noUnusedLocals.

Fixes #20568.
  • Loading branch information
mattmccutchen authored and mhegazy committed Jan 26, 2018
1 parent 9677b06 commit f0ba16c
Show file tree
Hide file tree
Showing 25 changed files with 283 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20967,7 +20967,7 @@ namespace ts {
error(name, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolName(local));
}
}
else if (compilerOptions.noUnusedLocals) {
else if (local.flags & SymbolFlags.TypeParameter ? compilerOptions.noUnusedParameters : compilerOptions.noUnusedLocals) {
forEach(local.declarations, d => errorUnusedLocal(d, symbolName(local)));
}
}
Expand Down Expand Up @@ -21042,7 +21042,7 @@ namespace ts {
}

function checkUnusedTypeParameters(node: ClassDeclaration | ClassExpression | FunctionDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction | ConstructorDeclaration | SignatureDeclaration | InterfaceDeclaration | TypeAliasDeclaration) {
if (compilerOptions.noUnusedLocals && !(node.flags & NodeFlags.Ambient)) {
if (compilerOptions.noUnusedParameters && !(node.flags & NodeFlags.Ambient)) {
if (node.typeParameters) {
// Only report errors on the last declaration for the type parameter container;
// this ensures that all uses have been accounted for.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
tests/cases/compiler/unusedTypeParametersCheckedByNoUnusedParameters.ts(1,12): error TS6133: 'T' is declared but its value is never read.
tests/cases/compiler/unusedTypeParametersCheckedByNoUnusedParameters.ts(3,8): error TS6133: 'T' is declared but its value is never read.
tests/cases/compiler/unusedTypeParametersCheckedByNoUnusedParameters.ts(5,13): error TS6133: 'T' is declared but its value is never read.
tests/cases/compiler/unusedTypeParametersCheckedByNoUnusedParameters.ts(7,9): error TS6133: 'T' is declared but its value is never read.
tests/cases/compiler/unusedTypeParametersCheckedByNoUnusedParameters.ts(8,14): error TS6133: 'V' is declared but its value is never read.
tests/cases/compiler/unusedTypeParametersCheckedByNoUnusedParameters.ts(11,10): error TS6133: 'T' is declared but its value is never read.


==== tests/cases/compiler/unusedTypeParametersCheckedByNoUnusedParameters.ts (6 errors) ====
function f<T>() { }
~
!!! error TS6133: 'T' is declared but its value is never read.

type T<T> = { };
~
!!! error TS6133: 'T' is declared but its value is never read.

interface I<T> { };
~
!!! error TS6133: 'T' is declared but its value is never read.

class C<T> {
~
!!! error TS6133: 'T' is declared but its value is never read.
public m<V>() { }
~
!!! error TS6133: 'V' is declared but its value is never read.
};

let l = <T>() => { };
~
!!! error TS6133: 'T' is declared but its value is never read.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [unusedTypeParametersCheckedByNoUnusedParameters.ts]
function f<T>() { }

type T<T> = { };

interface I<T> { };

class C<T> {
public m<V>() { }
};

let l = <T>() => { };


//// [unusedTypeParametersCheckedByNoUnusedParameters.js]
function f() { }
;
var C = /** @class */ (function () {
function C() {
}
C.prototype.m = function () { };
return C;
}());
;
var l = function () { };
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/unusedTypeParametersCheckedByNoUnusedParameters.ts ===
function f<T>() { }
>f : Symbol(f, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 0, 0))
>T : Symbol(T, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 0, 11))

type T<T> = { };
>T : Symbol(T, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 0, 19))
>T : Symbol(T, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 2, 7))

interface I<T> { };
>I : Symbol(I, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 2, 16))
>T : Symbol(T, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 4, 12))

class C<T> {
>C : Symbol(C, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 4, 19))
>T : Symbol(T, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 6, 8))

public m<V>() { }
>m : Symbol(C.m, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 6, 12))
>V : Symbol(V, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 7, 13))

};

let l = <T>() => { };
>l : Symbol(l, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 10, 3))
>T : Symbol(T, Decl(unusedTypeParametersCheckedByNoUnusedParameters.ts, 10, 9))

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/unusedTypeParametersCheckedByNoUnusedParameters.ts ===
function f<T>() { }
>f : <T>() => void
>T : T

type T<T> = { };
>T : {}
>T : T

interface I<T> { };
>I : I<T>
>T : T

class C<T> {
>C : C<T>
>T : T

public m<V>() { }
>m : <V>() => void
>V : V

};

let l = <T>() => { };
>l : <T>() => void
><T>() => { } : <T>() => void
>T : T

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [unusedTypeParametersNotCheckedByNoUnusedLocals.ts]
function f<T>() { }

type T<T> = { };

interface I<T> { };

class C<T> {
public m<V>() { }
};

let l = <T>() => { };


//// [unusedTypeParametersNotCheckedByNoUnusedLocals.js]
function f() { }
;
var C = /** @class */ (function () {
function C() {
}
C.prototype.m = function () { };
return C;
}());
;
var l = function () { };
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/unusedTypeParametersNotCheckedByNoUnusedLocals.ts ===
function f<T>() { }
>f : Symbol(f, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 0, 0))
>T : Symbol(T, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 0, 11))

type T<T> = { };
>T : Symbol(T, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 0, 19))
>T : Symbol(T, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 2, 7))

interface I<T> { };
>I : Symbol(I, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 2, 16))
>T : Symbol(T, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 4, 12))

class C<T> {
>C : Symbol(C, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 4, 19))
>T : Symbol(T, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 6, 8))

public m<V>() { }
>m : Symbol(C.m, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 6, 12))
>V : Symbol(V, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 7, 13))

};

let l = <T>() => { };
>l : Symbol(l, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 10, 3))
>T : Symbol(T, Decl(unusedTypeParametersNotCheckedByNoUnusedLocals.ts, 10, 9))

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/unusedTypeParametersNotCheckedByNoUnusedLocals.ts ===
function f<T>() { }
>f : <T>() => void
>T : T

type T<T> = { };
>T : {}
>T : T

interface I<T> { };
>I : I<T>
>T : T

class C<T> {
>C : C<T>
>T : T

public m<V>() { }
>m : <V>() => void
>V : V

};

let l = <T>() => { };
>l : <T>() => void
><T>() => { } : <T>() => void
>T : T

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(1,16): error TS6133:
tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(3,12): error TS6133: 'U' is declared but its value is never read.
tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(5,17): error TS6133: 'U' is declared but its value is never read.
tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(7,13): error TS6133: 'U' is declared but its value is never read.
tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(8,18): error TS6133: 'W' is declared but its value is never read.
tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(11,14): error TS6133: 'U' is declared but its value is never read.


==== tests/cases/compiler/unusedTypeParametersWithUnderscore.ts (4 errors) ====
==== tests/cases/compiler/unusedTypeParametersWithUnderscore.ts (6 errors) ====
function f<_T, U>() { }
~
!!! error TS6133: 'U' is declared but its value is never read.
Expand All @@ -17,7 +19,15 @@ tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(7,13): error TS6133:
~
!!! error TS6133: 'U' is declared but its value is never read.

class C<_T, U> { };
class C<_T, U> {
~
!!! error TS6133: 'U' is declared but its value is never read.
public m<_V, W>() { }
~
!!! error TS6133: 'W' is declared but its value is never read.
};

let l = <_T, U>() => { };
~
!!! error TS6133: 'U' is declared but its value is never read.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ type T<_T, U> = { };

interface I<_T, U> { };

class C<_T, U> { };
class C<_T, U> {
public m<_V, W>() { }
};

let l = <_T, U>() => { };


//// [unusedTypeParametersWithUnderscore.js]
Expand All @@ -14,6 +18,8 @@ function f() { }
var C = /** @class */ (function () {
function C() {
}
C.prototype.m = function () { };
return C;
}());
;
var l = function () { };
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ interface I<_T, U> { };
>_T : Symbol(_T, Decl(unusedTypeParametersWithUnderscore.ts, 4, 12))
>U : Symbol(U, Decl(unusedTypeParametersWithUnderscore.ts, 4, 15))

class C<_T, U> { };
class C<_T, U> {
>C : Symbol(C, Decl(unusedTypeParametersWithUnderscore.ts, 4, 23))
>_T : Symbol(_T, Decl(unusedTypeParametersWithUnderscore.ts, 6, 8))
>U : Symbol(U, Decl(unusedTypeParametersWithUnderscore.ts, 6, 11))

public m<_V, W>() { }
>m : Symbol(C.m, Decl(unusedTypeParametersWithUnderscore.ts, 6, 16))
>_V : Symbol(_V, Decl(unusedTypeParametersWithUnderscore.ts, 7, 13))
>W : Symbol(W, Decl(unusedTypeParametersWithUnderscore.ts, 7, 16))

};

let l = <_T, U>() => { };
>l : Symbol(l, Decl(unusedTypeParametersWithUnderscore.ts, 10, 3))
>_T : Symbol(_T, Decl(unusedTypeParametersWithUnderscore.ts, 10, 9))
>U : Symbol(U, Decl(unusedTypeParametersWithUnderscore.ts, 10, 12))

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ interface I<_T, U> { };
>_T : _T
>U : U

class C<_T, U> { };
class C<_T, U> {
>C : C<_T, U>
>_T : _T
>U : U

public m<_V, W>() { }
>m : <_V, W>() => void
>_V : _V
>W : W

};

let l = <_T, U>() => { };
>l : <_T, U>() => void
><_T, U>() => { } : <_T, U>() => void
>_T : _T
>U : U

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@noUnusedParameters:true

function f<T>() { }

type T<T> = { };

interface I<T> { };

class C<T> {
public m<V>() { }
};

let l = <T>() => { };
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@noUnusedLocals:true

function f<T>() { }

type T<T> = { };

interface I<T> { };

class C<T> {
public m<V>() { }
};

let l = <T>() => { };
8 changes: 6 additions & 2 deletions tests/cases/compiler/unusedTypeParametersWithUnderscore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//@noUnusedLocals:true
//@noUnusedParameters:true

function f<_T, U>() { }

type T<_T, U> = { };

interface I<_T, U> { };

class C<_T, U> { };
class C<_T, U> {
public m<_V, W>() { }
};

let l = <_T, U>() => { };
2 changes: 1 addition & 1 deletion tests/cases/fourslash/unusedTypeParametersInClass1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference path='fourslash.ts' />

// @noUnusedLocals: true
// @noUnusedParameters: true
////[|class greeter<T> |] {
////}

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/unusedTypeParametersInClass2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference path='fourslash.ts' />

// @noUnusedLocals: true
// @noUnusedParameters: true
////[|class greeter<X, Y> |] {
//// public a: X;
////}
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/unusedTypeParametersInClass3.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/// <reference path='fourslash.ts' />

// @noUnusedLocals: true
// @noUnusedParameters: true
////[|class greeter<X, Y, Z> |] {
//// public a: X;
//// public b: Z;
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/unusedTypeParametersInFunction1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference path='fourslash.ts' />

// @noUnusedLocals: true
// @noUnusedParameters: true
//// [|function f1<T>() {}|]

verify.codeFix({
Expand Down
Loading

0 comments on commit f0ba16c

Please sign in to comment.