diff --git a/linter/src/TypeScriptLinter.ts b/linter/src/TypeScriptLinter.ts index ee9c17e1a03949bf17c7f689d3a5de4b45011396..164ea1af70d8eb363c9ab1d45296d629500db3e5 100644 --- a/linter/src/TypeScriptLinter.ts +++ b/linter/src/TypeScriptLinter.ts @@ -894,7 +894,10 @@ export class TypeScriptLinter { tsBinaryExpr.operatorToken.kind === ts.SyntaxKind.GreaterThanGreaterThanToken || tsBinaryExpr.operatorToken.kind === ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken ) { - if (!(this.tsUtils.isNumberType(leftOperandType) && this.tsUtils.isNumberType(rightOperandType))) + if (!(this.tsUtils.isNumberType(leftOperandType) && this.tsUtils.isNumberType(rightOperandType)) || + ( tsLhsExpr.kind === ts.SyntaxKind.NumericLiteral && !this.tsUtils.isIntegerConstantValue(tsLhsExpr as ts.NumericLiteral)) || + ( tsRhsExpr.kind === ts.SyntaxKind.NumericLiteral && !this.tsUtils.isIntegerConstantValue(tsRhsExpr as ts.NumericLiteral)) + ) this.incrementCounters(node, FaultID.BitOpWithWrongType); } else if (tsBinaryExpr.operatorToken.kind === ts.SyntaxKind.CommaToken) { // CommaOpertor is allowed in 'for' statement initalizer and incrementor diff --git a/linter/test/binary_wrong_types.ts b/linter/test/binary_wrong_types.ts index 976ed7a1fee02d3d0f308f867ebe8e150ba5508f..e62f9d37c094dd856896ef41c76bc42034b75e61 100644 --- a/linter/test/binary_wrong_types.ts +++ b/linter/test/binary_wrong_types.ts @@ -97,3 +97,10 @@ const a000 = ((k = 10), 2 + 7); function foo(n: number, m: number): number { return (n/m) + ((n%m) ? 1 : 0); } + +const b40 = 7 & 5.5; +const b41 = 5.5 & b40; +const b42 = 2 | 5.5; +const b43 = 5.5 | b42; +const b44 = 4 ^ 5.5; +const b45 = 5.5 ^ b44; diff --git a/linter/test/binary_wrong_types.ts.relax.json b/linter/test/binary_wrong_types.ts.relax.json index c79d4040903b33eb2fc45f1e636e7daeee101c6f..f5197b0ef7ad821b8067eb5ca8156605a15a83f7 100644 --- a/linter/test/binary_wrong_types.ts.relax.json +++ b/linter/test/binary_wrong_types.ts.relax.json @@ -138,6 +138,36 @@ "line": 95, "column": 15, "problem": "CommaOperator" + }, + { + "line": 101, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 102, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 103, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 104, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 105, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 106, + "column": 13, + "problem": "BitOpWithWrongType" } ] } \ No newline at end of file diff --git a/linter/test/binary_wrong_types.ts.strict.json b/linter/test/binary_wrong_types.ts.strict.json index c79d4040903b33eb2fc45f1e636e7daeee101c6f..f5197b0ef7ad821b8067eb5ca8156605a15a83f7 100644 --- a/linter/test/binary_wrong_types.ts.strict.json +++ b/linter/test/binary_wrong_types.ts.strict.json @@ -138,6 +138,36 @@ "line": 95, "column": 15, "problem": "CommaOperator" + }, + { + "line": 101, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 102, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 103, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 104, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 105, + "column": 13, + "problem": "BitOpWithWrongType" + }, + { + "line": 106, + "column": 13, + "problem": "BitOpWithWrongType" } ] } \ No newline at end of file