Skip to content

Commit f9ae2cd

Browse files
hardfistAmour1688
authored andcommitted
chore: enable staticcheck rule (#150)
1 parent 02af1ca commit f9ae2cd

File tree

17 files changed

+55
-51
lines changed

17 files changed

+55
-51
lines changed

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ linters:
5656
# - gocritic
5757
# - gosec
5858
# - revive
59-
# - staticcheck
59+
- staticcheck
6060
# - testifylint
6161
# - unparam
62-
# - unused
62+
- unused
6363

6464
settings:
6565
depguard:

cmd/rslint/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ func printDiagnosticJsonLine(d rule.RuleDiagnostic, w *bufio.Writer, comparePath
141141
Error string `json:"error"`
142142
}
143143
errorObject := ErrorObject{Error: fmt.Sprintf("Failed to marshal diagnostic: %s", err)}
144-
145-
errorBytes,_ := json.Marshal(errorObject) //nolint:errchkjson
144+
145+
errorBytes, _ := json.Marshal(errorObject) //nolint:errchkjson
146146
w.Write(errorBytes)
147147
w.WriteByte('\n')
148148
return

cmd/rslint/lsp.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (s *LSPServer) handleInitialize(ctx context.Context, req *jsonrpc2.Request)
9797
Message: "Failed to parse initialize params",
9898
}
9999
}
100-
100+
//nolint:staticcheck
101101
if params.RootUri.DocumentUri != nil {
102102
s.rootURI = uriToPath(string(*params.RootUri.DocumentUri))
103103
}
@@ -274,7 +274,6 @@ func (s *LSPServer) runDiagnostics(ctx context.Context, uri lsproto.DocumentUri,
274274

275275
// Create multiple programs for all tsconfig files
276276
var programs []*compiler.Program
277-
var allSourceFiles []*ast.SourceFile
278277
var targetFile *ast.SourceFile
279278

280279
for _, tsConfigPath := range tsConfigs {
@@ -287,7 +286,6 @@ func (s *LSPServer) runDiagnostics(ctx context.Context, uri lsproto.DocumentUri,
287286

288287
// Check if the current file is in this program
289288
sourceFiles := program.GetSourceFiles()
290-
allSourceFiles = append(allSourceFiles, sourceFiles...)
291289

292290
if targetFile == nil {
293291
for _, sf := range sourceFiles {
@@ -451,10 +449,13 @@ func runLintWithPrograms(uri lsproto.DocumentUri, programs []*compiler.Program,
451449

452450
// Helper function to check if two ranges overlap
453451
func rangesOverlap(a, b lsproto.Range) bool {
454-
return !(a.End.Line < b.Start.Line ||
455-
(a.End.Line == b.Start.Line && a.End.Character < b.Start.Character) ||
456-
b.End.Line < a.Start.Line ||
457-
(b.End.Line == a.Start.Line && b.End.Character < a.Start.Character))
452+
// Ranges overlap if a starts before or at b's end AND b starts before or at a's end
453+
aStartsBefore := a.Start.Line < b.End.Line ||
454+
(a.Start.Line == b.End.Line && a.Start.Character <= b.End.Character)
455+
bStartsBefore := b.Start.Line < a.End.Line ||
456+
(b.Start.Line == a.End.Line && b.Start.Character <= a.End.Character)
457+
458+
return aStartsBefore && bStartsBefore
458459
}
459460

460461
// Helper function to create a code action from a rule diagnostic

internal/rules/no_base_to_string/no_base_to_string.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,12 @@ var NoBaseToStringRule = rule.Rule{
305305
if ast.IsPropertyAccessExpression(callExpr.Expression) {
306306
memberExpr := callExpr.Expression.AsPropertyAccessExpression()
307307
propertyName := memberExpr.Name().Text()
308-
if propertyName == "join" {
308+
switch propertyName {
309+
case "join":
309310
t := utils.GetConstrainedTypeAtLocation(ctx.TypeChecker, memberExpr.Expression)
310311
checkExpressionForArrayJoin(memberExpr.Expression, t)
311312
return
312-
} else if propertyName == "toLocaleString" || propertyName == "toString" {
313+
case "toLocaleString", "toString":
313314
checkExpression(memberExpr.Expression, nil)
314315
return
315316
}

internal/rules/no_duplicate_type_constituents/no_duplicate_type_constituents.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ var NoDuplicateTypeConstituentsRule = rule.Rule{
9191
prevStart := 0
9292
bracketBeforeTokens := []core.TextRange{}
9393

94-
for {
95-
if s.TokenStart() >= constituentNode.Pos() {
96-
break
97-
}
94+
for s.TokenStart() < constituentNode.Pos() {
95+
9896
if s.Token() == ast.KindAmpersandToken || s.Token() == ast.KindBarToken {
9997
foundBefore = true
10098
prevStart = s.TokenStart()
@@ -238,13 +236,14 @@ var NoDuplicateTypeConstituentsRule = rule.Rule{
238236

239237
var unionOrIntersection unionOrIntersection
240238
var types []*ast.Node
241-
if node.Kind == ast.KindIntersectionType {
239+
switch node.Kind {
240+
case ast.KindIntersectionType:
242241
unionOrIntersection = unionOrIntersection_Intersection
243242
types = node.AsIntersectionTypeNode().Types.Nodes
244-
} else if node.Kind == ast.KindUnionType {
243+
case ast.KindUnionType:
245244
unionOrIntersection = unionOrIntersection_Union
246245
types = node.AsUnionTypeNode().Types.Nodes
247-
} else {
246+
default:
248247
panic(fmt.Sprintf("expected union or intersection, got %v", node.Kind))
249248
}
250249

@@ -288,7 +287,6 @@ var NoDuplicateTypeConstituentsRule = rule.Rule{
288287
report(true, unionOrIntersection_Union, buildUnnecessaryMessage(), constituentNode)
289288
}
290289
})
291-
return
292290
}
293291
}
294292

internal/rules/no_misused_promises/no_misused_promises.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ var NoMisusedPromisesRule = rule.Rule{
508508
// signature here against its compatible index signatures in `heritageTypes`
509509
continue
510510
}
511-
if !(ast.IsIdentifier(nodeMember.Name()) || ast.IsPrivateIdentifier(nodeMember.Name()) || ast.IsStringLiteral(nodeMember.Name()) || ast.IsNumericLiteral(nodeMember.Name()) || ast.IsBigIntLiteral(nodeMember.Name())) {
511+
if !ast.IsIdentifier(nodeMember.Name()) && !ast.IsPrivateIdentifier(nodeMember.Name()) && !ast.IsStringLiteral(nodeMember.Name()) && !ast.IsNumericLiteral(nodeMember.Name()) && !ast.IsBigIntLiteral(nodeMember.Name()) {
512512
continue
513513
}
514514
memberName := nodeMember.Name().Text()
@@ -593,7 +593,9 @@ var NoMisusedPromisesRule = rule.Rule{
593593
)
594594

595595
if isVoidReturningFunctionType(node.Name(), contextualType) {
596+
//nolint:staticcheck // FIXME: todo
596597
if ast.IsMethodDeclaration(node) {
598+
597599
}
598600

599601
if node.Type() != nil {

internal/rules/no_unnecessary_type_arguments/no_unnecessary_type_arguments.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ var NoUnnecessaryTypeArgumentsRule = rule.Rule{
124124
if utils.IsIntrinsicErrorType(argType) {
125125
return
126126
}
127-
if argType != paramType && (utils.IsTypeAnyType(argType) || utils.IsTypeAnyType(paramType) || !(checker.Checker_isTypeStrictSubtypeOf(ctx.TypeChecker, argType, paramType) && checker.Checker_isTypeStrictSubtypeOf(ctx.TypeChecker, paramType, argType))) {
127+
if argType != paramType && (utils.IsTypeAnyType(argType) || utils.IsTypeAnyType(paramType) || (!checker.Checker_isTypeStrictSubtypeOf(ctx.TypeChecker, argType, paramType) || !checker.Checker_isTypeStrictSubtypeOf(ctx.TypeChecker, paramType, argType))) {
128128
return
129129
}
130130

internal/rules/no_unnecessary_type_assertion/no_unnecessary_type_assertion.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ var NoUnnecessaryTypeAssertionRule = rule.Rule{
107107
t := utils.GetConstrainedTypeAtLocation(ctx.TypeChecker, node)
108108
if declarationType == t &&
109109
// `declare`s are never narrowed, so never skip them
110-
!(ast.IsVariableDeclarationList(declaration.Parent) &&
111-
ast.IsVariableStatement(declaration.Parent.Parent) &&
112-
utils.IncludesModifier(declaration.Parent.Parent.AsVariableStatement(), ast.KindDeclareKeyword)) {
110+
(!ast.IsVariableDeclarationList(declaration.Parent) || !ast.IsVariableStatement(declaration.Parent.Parent) || !utils.IncludesModifier(declaration.Parent.Parent.AsVariableStatement(), ast.KindDeclareKeyword)) {
113111
// possibly used before assigned, so just skip it
114112
// better to false negative and skip it, than false positive and fix to compile erroring code
115113
//

internal/rules/no_unsafe_argument/no_unsafe_argument.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func newFunctionSignature(
6666
for i, param := range parameters {
6767
t := typeChecker.GetTypeOfSymbolAtLocation(param, node)
6868

69-
if param.Declarations != nil && len(param.Declarations) != 0 {
69+
if len(param.Declarations) != 0 {
7070
decl := param.Declarations[0]
7171
if utils.IsRestParameterDeclaration(decl) {
7272
// is a rest param
@@ -240,7 +240,10 @@ var NoUnsafeArgumentRule = rule.Rule{
240240
// all remaining arguments should be compared against the rest type (if one exists)
241241
signature.consumeRemainingArguments()
242242
}
243-
} else {
243+
244+
} else
245+
//nolint:staticcheck // FIXME: todo
246+
{
244247
// something that's iterable
245248
// handling this will be pretty complex - so we ignore it for now
246249
// TODO - handle generic iterable case

internal/rules/no_unsafe_enum_comparison/no_unsafe_enum_comparison.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ var NoUnsafeEnumComparisonRule = rule.Rule{
134134
ast.KindBinaryExpression: func(node *ast.Node) {
135135
expr := node.AsBinaryExpression()
136136
opKind := expr.OperatorToken.Kind
137-
if !(opKind == ast.KindLessThanToken || opKind == ast.KindLessThanEqualsToken || opKind == ast.KindGreaterThanToken || opKind == ast.KindGreaterThanEqualsToken || opKind == ast.KindEqualsEqualsToken || opKind == ast.KindEqualsEqualsEqualsToken || opKind == ast.KindExclamationEqualsToken || opKind == ast.KindExclamationEqualsEqualsToken) {
137+
if opKind != ast.KindLessThanToken && opKind != ast.KindLessThanEqualsToken && opKind != ast.KindGreaterThanToken && opKind != ast.KindGreaterThanEqualsToken && opKind != ast.KindEqualsEqualsToken && opKind != ast.KindEqualsEqualsEqualsToken && opKind != ast.KindExclamationEqualsToken && opKind != ast.KindExclamationEqualsEqualsToken {
138138
return
139139
}
140140

0 commit comments

Comments
 (0)