Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var NoConstantBinaryExpressionRule = rule.Rule{ Name: "no-constant-binary-expression", Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { return rule.RuleListeners{ ast.KindBinaryExpression: func(node *ast.Node) { binary := node.AsBinaryExpression() if binary == nil || binary.OperatorToken == nil { return } operator := binary.OperatorToken.Kind if operator == ast.KindAmpersandAmpersandToken || operator == ast.KindBarBarToken { if isConstant(&ctx, binary.Left, true) { var prop string if operator == ast.KindAmpersandAmpersandToken { prop = "&&" } else { prop = "||" } ctx.ReportNode(node, buildConstantShortCircuitMessage(prop)) } return } if operator == ast.KindQuestionQuestionToken { if hasConstantNullishness(&ctx, binary.Left, false) { ctx.ReportNode(node, buildConstantShortCircuitMessage("??")) } return } switch operator { case ast.KindEqualsEqualsToken, ast.KindExclamationEqualsToken, ast.KindEqualsEqualsEqualsToken, ast.KindExclamationEqualsEqualsToken: rightConstant := findBinaryExpressionConstantOperand(&ctx, binary.Left, binary.Right, operator) leftConstant := findBinaryExpressionConstantOperand(&ctx, binary.Right, binary.Left, operator) if rightConstant != nil { ctx.ReportNode(node, buildConstantBinaryOperandMessage()) } else if leftConstant != nil { ctx.ReportNode(node, buildConstantBinaryOperandMessage()) } else if operator == ast.KindEqualsEqualsEqualsToken || operator == ast.KindExclamationEqualsEqualsToken { if isAlwaysNew(&ctx, binary.Left) { ctx.ReportNode(node, buildAlwaysNewMessage()) } else if isAlwaysNew(&ctx, binary.Right) { ctx.ReportNode(node, buildAlwaysNewMessage()) } } else { if isAlwaysNew(&ctx, binary.Left) && isAlwaysNew(&ctx, binary.Right) { ctx.ReportNode(node, buildBothAlwaysNewMessage()) } } } }, } }, }
NoConstantBinaryExpressionRule detects constant binary expressions
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.