-
-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New diagnostic type, documentation, and test for if("x" || "y"), case "x" || "y": #993
base: master
Are you sure you want to change the base?
Changes from all commits
8896052
3495612
bb3e149
28ed373
d2fd801
33c764e
6d989a4
481d5ed
862d01d
a56a57a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# E0373: Pointless Comparison Against String Literal | ||
|
||
A condition with a string literal will always evaluate to true | ||
|
||
```typescript | ||
if("x" || "y"){ | ||
let x = y; | ||
} | ||
``` | ||
|
||
To fix this warning, remove the condition. | ||
|
||
```typescript | ||
let x = y; | ||
``` | ||
|
||
Or, | ||
|
||
```typescript | ||
case "x" || "y": | ||
``` | ||
Comment on lines
+19
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must fix: Examples with bad code belong in the first code block. Also, they must be syntactically valid aside from the diagnostic, so you must include a |
||
|
||
To fix this warning, make separate cases | ||
|
||
```typescript | ||
case "x": | ||
|
||
case "y": | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2960,6 +2960,16 @@ | |
"expression literal always returns '{1}'"), \ | ||
equals_operator, comparison_result)) \ | ||
\ | ||
QLJS_DIAG_TYPE( \ | ||
diag_pointless_comp_against_string_expression_literal, "E0373", \ | ||
diagnostic_severity::warning, \ | ||
{ \ | ||
source_code_span or_operator; \ | ||
}, \ | ||
MESSAGE(QLJS_TRANSLATABLE("comparison with a non-empty string literal " \ | ||
" always returns '{1}'"), \ | ||
or_operator)) \ | ||
\ | ||
QLJS_DIAG_TYPE( \ | ||
diag_unexpected_function_parameter_is_parenthesized, "E0349", \ | ||
diagnostic_severity::error, \ | ||
|
@@ -2976,13 +2986,6 @@ | |
diag_unexpected_colon_after_generic_definition, "E0331", \ | ||
diagnostic_severity::error, { source_code_span colon; }, \ | ||
MESSAGE(QLJS_TRANSLATABLE("':' should be 'extends' instead"), colon)) \ | ||
\ | ||
QLJS_DIAG_TYPE( \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must fix: I think you reverted someone else's commit by mistake (edec089?). Reintroduce this diagnostic. |
||
diag_pointless_nullish_coalescing_operator, "E0369", \ | ||
diagnostic_severity::warning, { source_code_span question_question; }, \ | ||
MESSAGE(QLJS_TRANSLATABLE("nullish coalescing operator does nothing " \ | ||
"when left operand is never null"), \ | ||
question_question)) \ | ||
/* END */ | ||
|
||
// QLJS_X_RESERVED_DIAG_TYPES lists reserved error codes. These codes were used | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,6 +334,39 @@ void parser::error_on_sketchy_condition(expression* ast) { | |
} | ||
} | ||
|
||
void parser::warn_on_pointless_string_condition( | ||
expression::conditional* ast) { | ||
auto is_or_operator = [](string8_view s) { | ||
return s == u8"||"_sv; | ||
}; | ||
|
||
for (span_size i = 0; i < ast->child_count() + 1; i++) { | ||
expression* lhs = ast->child(i)->without_paren(); | ||
expression* rhs = ast->child(i + 1)->without_paren(); | ||
Comment on lines
+343
to
+345
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must fix: Iterating to I think you meant |
||
|
||
if ((lhs->kind() == expression_kind::literal && | ||
rhs->kind() == expression_kind::literal) ){ | ||
source_code_span op_span = ast->operator_spans_[i]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must fix: This code does not compile:
|
||
if (!is_or_operator(op_span.string_view())) { | ||
continue; | ||
} | ||
auto char_is_a_quote = [](const char8* s) { | ||
return *s == '"' || *s == '\'' || *s == '`'; | ||
}; | ||
|
||
if (!char_is_a_quote(rhs->span().begin())) { | ||
continue; | ||
} | ||
|
||
// string8_view literal1 = lhs->child_0()->variable_identifier().span().string_view(); | ||
// string8_view literal2 = rhs->span().string_view(); | ||
|
||
this->diag_reporter_->report( | ||
diag_pointless_comp_against_string_expression_literal{op_span}); | ||
} | ||
} | ||
} | ||
|
||
void parser::warn_on_comma_operator_in_conditional_statement(expression* ast) { | ||
if (ast->kind() != expression_kind::binary_operator) return; | ||
|
||
|
@@ -435,7 +468,6 @@ void parser::error_on_invalid_as_const(expression* ast, | |
case expression_kind::dot: | ||
case expression_kind::array: | ||
case expression_kind::object: | ||
case expression_kind::_template: | ||
break; | ||
|
||
case expression_kind::literal: { | ||
|
@@ -735,82 +767,6 @@ void parser::consume_semicolon() { | |
} | ||
} | ||
|
||
void parser::error_on_pointless_nullish_coalescing_operator( | ||
expression::binary_operator* ast) { | ||
auto is_nullish_operator = [](string8_view s) -> bool { | ||
return s == u8"??"_sv; | ||
}; | ||
|
||
for (span_size i = 0; i < ast->child_count() - 1; i++) { | ||
source_code_span op_span = ast->operator_spans_[i]; | ||
if (is_nullish_operator(op_span.string_view())) { | ||
if (i >= 1) { | ||
// lhs is a multi-child expression | ||
return; | ||
} else { | ||
this->check_lhs_for_null_potential(ast->child(i)->without_paren(), | ||
op_span); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void parser::check_lhs_for_null_potential(expression* lhs, | ||
source_code_span op_span) { | ||
auto binary_operator_is_never_null = | ||
[](expression::binary_operator* expr) -> bool { | ||
// these 4 binary operators can resolve to a null value | ||
string8_view can_resolve_to_null[4] = {u8"&&"_sv, u8"??"_sv, u8","_sv, | ||
u8"||"_sv}; | ||
for (span_size i = 0; i < expr->child_count() - 1; i++) { | ||
string8_view expr_op_span = expr->operator_spans_[i].string_view(); | ||
for (span_size j = 0; j < 4; j++) { | ||
if (expr_op_span == can_resolve_to_null[j]) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
bool report_diag = false; | ||
switch (lhs->kind()) { | ||
case expression_kind::literal: | ||
if (lhs->span().string_view() == u8"null"_sv) { | ||
break; | ||
} | ||
if (lhs->span().string_view() == u8"undefined"_sv) { | ||
break; | ||
} | ||
report_diag = true; | ||
break; | ||
case expression_kind::rw_unary_suffix: | ||
report_diag = true; | ||
break; | ||
case expression_kind::unary_operator: { | ||
auto* maybe_void_lhs = static_cast<expression::unary_operator*>(lhs); | ||
if (maybe_void_lhs->is_void_operator() == false) { | ||
report_diag = true; | ||
} | ||
break; | ||
} | ||
case expression_kind::_typeof: | ||
report_diag = true; | ||
break; | ||
case expression_kind::binary_operator: { | ||
auto* operator_lhs = static_cast<expression::binary_operator*>(lhs); | ||
report_diag = binary_operator_is_never_null(operator_lhs); | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
if (report_diag == true) { | ||
this->diag_reporter_->report(diag_pointless_nullish_coalescing_operator{ | ||
.question_question = op_span}); | ||
} | ||
} | ||
|
||
template void | ||
parser::consume_semicolon<diag_missing_semicolon_after_abstract_method>(); | ||
template void | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: I prefer examples in documentation to be
javascript
if possible.