no_constant_binary_expression

package
v0.1.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoConstantBinaryExpressionRule = rule.CreateRule(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

				switch operator {
				case ast.KindAmpersandAmpersandToken:
					if isConstant(binary.Left) {
						ctx.ReportNode(node, buildConstantShortCircuitMessage("&&"))
					}
				case ast.KindBarBarToken:
					if isConstant(binary.Left) {
						ctx.ReportNode(node, buildConstantShortCircuitMessage("||"))
					}
				case ast.KindQuestionQuestionToken:
					if hasConstantNullishness(binary.Left) {
						ctx.ReportNode(node, buildConstantShortCircuitMessage("??"))
					}
				}

				switch operator {
				case ast.KindEqualsEqualsToken,
					ast.KindExclamationEqualsToken,
					ast.KindEqualsEqualsEqualsToken,
					ast.KindExclamationEqualsEqualsToken:

					if isAlwaysNew(binary.Left) && isAlwaysNew(binary.Right) {
						ctx.ReportNode(node, buildBothAlwaysNewMessage())
						return
					}

					if isAlwaysNew(binary.Left) && !isConstant(binary.Right) {
						ctx.ReportNode(node, buildAlwaysNewMessage())
						return
					}
					if isAlwaysNew(binary.Right) && !isConstant(binary.Left) {
						ctx.ReportNode(node, buildAlwaysNewMessage())
						return
					}

					constantOperand := findBinaryExpressionConstantOperand(binary.Left, binary.Right, operator)
					if constantOperand != nil {
						ctx.ReportNode(node, buildConstantBinaryOperandMessage())
					}
				}
			},
		}
	},
})

NoConstantBinaryExpressionRule detects constant binary expressions

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL