For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/unicorn/prefer-unary-minus.md.
close

prefer-unary-minus

Added in v0.10.1

Configuration

rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-unary-minus': 'error',
    },
  },
]);

Rule Details

Prefer the unary minus operator over multiplying or dividing by -1.

The rule reports value * -1, -1 * value, and value / -1. Division in the opposite direction (-1 / value) and expressions where both operands are -1 are left alone. A BigInt multiplier or divisor such as -1n does not match the numeric literal -1.

Examples of incorrect code for this rule:

const inverted = value * -1;
const flipped = -1 * someNumber;
const divided = value / -1;

Examples of correct code for this rule:

const inverted = -value;
const flipped = -someNumber;
const divided = -value;

Fixes

The fix negates the other operand, adding parentheses and spacing where needed. Comments inside that operand are preserved. If the fix would remove a comment around the operator or -1, the rule reports the expression without a fix.

Options

This rule has no options.

Differences from upstream

To lint a TypeScript type assertion followed by **, put parentheses around the assertion. For example, write (value * -1 as any) ** 2. The unparenthesized form value * -1 as any ** 2 produces a syntax error instead of this rule's diagnostic. The parenthesized form is checked and fixed normally.

Original Documentation