fix(linter): improve the help message for const-comparisons (#1764)

closes #1741
This commit is contained in:
Boshen 2023-12-21 16:04:39 +08:00 committed by GitHub
parent d0cc3ec9a2
commit 6d4202241b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 18 deletions

View file

@ -16,10 +16,10 @@ use crate::{context::LintContext, rule::Rule, utils::is_same_reference, AstNode}
#[derive(Debug, Error, Diagnostic)]
enum ConstComparisonsDiagnostic {
#[error("oxc(const-comparisons): Unexpected redundant comparison. Left-hand side of `&&` operator has no effect")]
#[error("oxc(const-comparisons): Left-hand side of `&&` operator has no effect.")]
#[diagnostic(severity(warning), help("{1}"))]
RedundantLeftHandSide(#[label] Span, String),
#[error("oxc(const-comparisons): Unexpected redundant comparison. Right-hand side of `&&` operator has no effect")]
#[error("oxc(const-comparisons): Right-hand side of `&&` operator has no effect.")]
#[diagnostic(severity(warning), help("{1}"))]
RedundantRightHandSide(#[label] Span, String),
#[error("oxc(const-comparisons): Unexpected constant comparison")]
@ -98,8 +98,8 @@ impl Rule for ConstComparisons {
}
if left_cmp_op.direction() == right_cmp_op.direction() {
let lhs_str = left_const_expr.value.to_string();
let rhs_str = right_const_expr.value.to_string();
let lhs_str = logical_expr.left.span().source_text(ctx.source_text());
let rhs_str = logical_expr.right.span().source_text(ctx.source_text());
// We already know that either side of `&&` has no effect,
// but emit a different error message depending on which side it is
if left_side_is_useless(left_cmp_op, ordering) {
@ -114,8 +114,8 @@ impl Rule for ConstComparisons {
));
}
} else if !comparison_is_possible(left_cmp_op.direction(), ordering) {
let lhs_str = left_const_expr.value.to_string();
let rhs_str = right_const_expr.value.to_string();
let lhs_str = left_const_expr.span.source_text(ctx.source_text());
let rhs_str = right_const_expr.span.source_text(ctx.source_text());
let expr_str = left_expr.span().source_text(ctx.source_text());
let diagnostic_note = match ordering {
Ordering::Less => format!(

View file

@ -51,46 +51,46 @@ expression: const_comparisons
╰────
help: since `500` < `600`, the expression evaluates to false for any value of `status`
⚠ oxc(const-comparisons): Unexpected redundant comparison. Right-hand side of `&&` operator has no effect
⚠ oxc(const-comparisons): Right-hand side of `&&` operator has no effect.
╭─[const_comparisons.tsx:1:1]
1 │ status_code < 200 && status_code <= 299;
· ───────────────────────────────────────
╰────
help: if `200` evaluates to true, `299` will always evaluate to true as well
help: if `status_code < 200` evaluates to true, `status_code <= 299` will always evaluate to true as well
⚠ oxc(const-comparisons): Unexpected redundant comparison. Left-hand side of `&&` operator has no effect
⚠ oxc(const-comparisons): Left-hand side of `&&` operator has no effect.
╭─[const_comparisons.tsx:1:1]
1 │ status_code > 200 && status_code >= 299;
· ───────────────────────────────────────
╰────
help: if `299` evaluates to true, `200` will always evaluate to true as well
help: if `status_code >= 299` evaluates to true, `status_code > 200` will always evaluate to true as well
⚠ oxc(const-comparisons): Unexpected redundant comparison. Left-hand side of `&&` operator has no effect
⚠ oxc(const-comparisons): Left-hand side of `&&` operator has no effect.
╭─[const_comparisons.tsx:1:1]
1 │ status_code >= 500 && status_code > 500;
· ───────────────────────────────────────
╰────
help: if `500` evaluates to true, `500` will always evaluate to true as well
help: if `status_code > 500` evaluates to true, `status_code >= 500` will always evaluate to true as well
⚠ oxc(const-comparisons): Unexpected redundant comparison. Right-hand side of `&&` operator has no effect
⚠ oxc(const-comparisons): Right-hand side of `&&` operator has no effect.
╭─[const_comparisons.tsx:1:1]
1 │ status_code > 500 && status_code >= 500;
· ───────────────────────────────────────
╰────
help: if `500` evaluates to true, `500` will always evaluate to true as well
help: if `status_code > 500` evaluates to true, `status_code >= 500` will always evaluate to true as well
⚠ oxc(const-comparisons): Unexpected redundant comparison. Left-hand side of `&&` operator has no effect
⚠ oxc(const-comparisons): Left-hand side of `&&` operator has no effect.
╭─[const_comparisons.tsx:1:1]
1 │ status_code <= 500 && status_code < 500;
· ───────────────────────────────────────
╰────
help: if `500` evaluates to true, `500` will always evaluate to true as well
help: if `status_code < 500` evaluates to true, `status_code <= 500` will always evaluate to true as well
⚠ oxc(const-comparisons): Unexpected redundant comparison. Right-hand side of `&&` operator has no effect
⚠ oxc(const-comparisons): Right-hand side of `&&` operator has no effect.
╭─[const_comparisons.tsx:1:1]
1 │ status_code < 500 && status_code <= 500;
· ───────────────────────────────────────
╰────
help: if `500` evaluates to true, `500` will always evaluate to true as well
help: if `status_code < 500` evaluates to true, `status_code <= 500` will always evaluate to true as well