return null constant value for unclosed char literals

a better way to fix IDEA-CR-36839 HardcodedFileSeparatorsInspection: don't fail on unclosed char literals
This commit is contained in:
peter
2018-09-14 12:07:49 +02:00
parent 9317647cba
commit 1c296d490e
4 changed files with 7 additions and 24 deletions
@@ -1036,27 +1036,15 @@ public class HighlightUtil extends HighlightUtilBase {
}
}
else if (type == JavaTokenType.CHARACTER_LITERAL) {
if (value != null) {
if (!StringUtil.endsWithChar(text, '\'')) {
String message = JavaErrorMessages.message("unclosed.char.literal");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
}
}
else {
if (value == null) {
if (!StringUtil.startsWithChar(text, '\'')) {
return null;
}
if (StringUtil.endsWithChar(text, '\'')) {
if (text.length() == 1) {
String message = JavaErrorMessages.message("illegal.line.end.in.character.literal");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
}
text = text.substring(1, text.length() - 1);
}
else {
String message = JavaErrorMessages.message("illegal.line.end.in.character.literal");
if (!StringUtil.endsWithChar(text, '\'') || text.length() == 1) {
String message = JavaErrorMessages.message("unclosed.char.literal");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
}
text = text.substring(1, text.length() - 1);
StringBuilder chars = new StringBuilder();
boolean success = PsiLiteralExpressionImpl.parseStringCharacters(text, chars, null);
@@ -134,13 +134,10 @@ public class PsiLiteralExpressionImpl
return PsiLiteralUtil.parseDouble(text);
}
if (type == JavaTokenType.CHARACTER_LITERAL) {
if (StringUtil.endsWithChar(text, '\'')) {
if (textLength == 1) return null;
text = text.substring(1, textLength - 1);
}
else {
text = text.substring(1, textLength);
if (textLength == 1 || !StringUtil.endsWithChar(text, '\'')) {
return null;
}
text = text.substring(1, textLength - 1);
StringBuilder chars = new StringBuilder();
boolean success = parseStringCharacters(text, chars, null);
if (!success) return null;
@@ -315,7 +315,6 @@ binary.numbers.must.contain.at.least.one.hexadecimal.digit=Binary numbers must c
integer.number.too.large=Integer number too large
long.number.too.large=Long number too large
malformed.floating.point.literal=Malformed floating point literal
illegal.line.end.in.character.literal=Illegal line end in character literal
illegal.escape.character.in.character.literal=Illegal escape character in character literal
too.many.characters.in.character.literal=Too many characters in character literal
empty.character.literal=Empty character literal
@@ -146,7 +146,6 @@ public class HardcodedFileSeparatorsInspection extends BaseInspection {
@Override
public void visitLiteralExpression(@NotNull PsiLiteralExpression expression) {
super.visitLiteralExpression(expression);
if (expression.getNextSibling() instanceof PsiErrorElement) return;
final PsiType type = expression.getType();
if (TypeUtils.isJavaLangString(type)) {