no_extra_boolean_cast

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoExtraBooleanCastRule = rule.Rule{
	Name: "no-extra-boolean-cast",
	Run: func(ctx rule.RuleContext, ruleOptions any) rule.RuleListeners {
		opts := parseOptions(ruleOptions)

		negationMsg := rule.RuleMessage{
			Id:          "unexpectedNegation",
			Description: "Redundant double negation.",
		}
		callMsg := rule.RuleMessage{
			Id:          "unexpectedCall",
			Description: "Redundant Boolean call.",
		}

		return rule.RuleListeners{

			ast.KindPrefixUnaryExpression: func(node *ast.Node) {
				prefix := node.AsPrefixUnaryExpression()
				if prefix == nil || prefix.Operator != ast.KindExclamationToken {
					return
				}
				operand := prefix.Operand
				if operand == nil || !ast.IsPrefixUnaryExpression(operand) {
					return
				}
				if operand.AsPrefixUnaryExpression().Operator != ast.KindExclamationToken {
					return
				}
				if !isInFlaggedContext(node, opts) {
					return
				}
				if fixes := buildNegationFix(ctx, node); fixes != nil {
					ctx.ReportNodeWithFixes(node, negationMsg, fixes...)
				} else {
					ctx.ReportNode(node, negationMsg)
				}
			},

			ast.KindCallExpression: func(node *ast.Node) {
				if !isBooleanFunctionOrConstructorCall(node) {
					return
				}
				if !isInFlaggedContext(node, opts) {
					return
				}
				if fixes := buildCallFix(ctx, node); fixes != nil {
					ctx.ReportNodeWithFixes(node, callMsg, fixes...)
				} else {
					ctx.ReportNode(node, callMsg)
				}
			},
		}
	},
}

NoExtraBooleanCastRule disallows unnecessary boolean casts. Reports `!!expr` (double negation) and `Boolean(expr)` calls in contexts that already coerce to boolean.

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