no_constant_binary_expression

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT, MIT Imports: 3 Imported by: 0

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.

Jump to

Keyboard shortcuts

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