Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public class IteratorNextDoesNotThrowNoSuchElementExceptionInspection extends BaseInspection {
@Nonnull
@Override
@Pattern("[a-zA-Z_0-9.]+")
@Pattern(VALID_ID_PATTERN)
public String getID() {
return "IteratorNextCanNotThrowNoSuchElementException";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,116 +25,116 @@
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.localize.InspectionGadgetsLocalize;
import consulo.annotation.component.ExtensionImpl;
import consulo.localize.LocalizeValue;
import jakarta.annotation.Nonnull;

@ExtensionImpl
public class MalformedFormatStringInspection extends BaseInspection {

@Override
@Nonnull
public String getDisplayName() {
return InspectionGadgetsLocalize.malformedFormatStringDisplayName().get();
}

@Override
@Nonnull
public String buildErrorString(Object... infos) {
final Object value = infos[0];
if (value instanceof Exception) {
return InspectionGadgetsLocalize.malformedFormatStringProblemDescriptorMalformed().get();
}
final Validator[] validators = (Validator[])value;
final int argumentCount = ((Integer)infos[1]).intValue();
if (validators.length < argumentCount) {
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.many.arguments");
@Nonnull
@Override
public LocalizeValue getDisplayName() {
return InspectionGadgetsLocalize.malformedFormatStringDisplayName();
}
if (validators.length > argumentCount) {
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.few.arguments");

@Override
@Nonnull
public String buildErrorString(Object... infos) {
final Object value = infos[0];
if (value instanceof Exception) {
return InspectionGadgetsLocalize.malformedFormatStringProblemDescriptorMalformed().get();
}
final Validator[] validators = (Validator[]) value;
final int argumentCount = ((Integer) infos[1]).intValue();
if (validators.length < argumentCount) {
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.many.arguments");
}
if (validators.length > argumentCount) {
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.few.arguments");
}
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.arguments.do.not.match.type");
}
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.arguments.do.not.match.type");
}

@Override
public boolean isEnabledByDefault() {
return true;
}
@Override
public boolean isEnabledByDefault() {
return true;
}

@Override
public BaseInspectionVisitor buildVisitor() {
return new MalformedFormatStringVisitor();
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new MalformedFormatStringVisitor();
}

private static class MalformedFormatStringVisitor extends BaseInspectionVisitor {
private static class MalformedFormatStringVisitor extends BaseInspectionVisitor {

@Override
public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
if (!FormatUtils.isFormatCall(expression)) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
final PsiExpression firstArgument = arguments[0];
final PsiType type = firstArgument.getType();
if (type == null) {
return;
}
final int formatArgumentIndex;
if ("java.util.Locale".equals(type.getCanonicalText()) && arguments.length > 1) {
formatArgumentIndex = 1;
}
else {
formatArgumentIndex = 0;
}
final PsiExpression formatArgument = arguments[formatArgumentIndex];
if (!ExpressionUtils.hasStringType(formatArgument)) {
return;
}
if (!PsiUtil.isConstantExpression(formatArgument)) {
return;
}
final PsiType formatType = formatArgument.getType();
if (formatType == null) {
return;
}
final String value = (String)ConstantExpressionUtil.computeCastTo(formatArgument, formatType);
if (value == null) {
return;
}
final int argumentCount = arguments.length - (formatArgumentIndex + 1);
final Validator[] validators;
try {
validators = FormatDecode.decode(value, argumentCount);
}
catch (Exception e) {
registerError(formatArgument, e);
return;
}
if (validators.length != argumentCount) {
if (argumentCount == 1) {
final PsiExpression argument = arguments[formatArgumentIndex + 1];
final PsiType argumentType = argument.getType();
if (argumentType instanceof PsiArrayType) {
return;
}
}
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
return;
}
for (int i = 0; i < validators.length; i++) {
final Validator validator = validators[i];
final PsiType argumentType = arguments[i + formatArgumentIndex + 1].getType();
if (argumentType == null) {
continue;
}
if (!validator.valid(argumentType)) {
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
return;
@Override
public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
if (!FormatUtils.isFormatCall(expression)) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
final PsiExpression firstArgument = arguments[0];
final PsiType type = firstArgument.getType();
if (type == null) {
return;
}
final int formatArgumentIndex;
if ("java.util.Locale".equals(type.getCanonicalText()) && arguments.length > 1) {
formatArgumentIndex = 1;
}
else {
formatArgumentIndex = 0;
}
final PsiExpression formatArgument = arguments[formatArgumentIndex];
if (!ExpressionUtils.hasStringType(formatArgument)) {
return;
}
if (!PsiUtil.isConstantExpression(formatArgument)) {
return;
}
final PsiType formatType = formatArgument.getType();
if (formatType == null) {
return;
}
final String value = (String) ConstantExpressionUtil.computeCastTo(formatArgument, formatType);
if (value == null) {
return;
}
final int argumentCount = arguments.length - (formatArgumentIndex + 1);
final Validator[] validators;
try {
validators = FormatDecode.decode(value, argumentCount);
}
catch (Exception e) {
registerError(formatArgument, e);
return;
}
if (validators.length != argumentCount) {
if (argumentCount == 1) {
final PsiExpression argument = arguments[formatArgumentIndex + 1];
final PsiType argumentType = argument.getType();
if (argumentType instanceof PsiArrayType) {
return;
}
}
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
return;
}
for (int i = 0; i < validators.length; i++) {
final Validator validator = validators[i];
final PsiType argumentType = arguments[i + formatArgumentIndex + 1].getType();
if (argumentType == null) {
continue;
}
if (!validator.valid(argumentType)) {
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
return;
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,76 +27,76 @@
import com.siyeh.ig.psiutils.MethodCallUtils;
import com.siyeh.localize.InspectionGadgetsLocalize;
import consulo.annotation.component.ExtensionImpl;
import consulo.localize.LocalizeValue;
import jakarta.annotation.Nonnull;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

@ExtensionImpl
public class MalformedRegexInspection extends BaseInspection {
@Nonnull
@Override
public LocalizeValue getDisplayName() {
return InspectionGadgetsLocalize.malformedRegularExpressionDisplayName();
}

@Override
@Nonnull
public String getDisplayName() {
return InspectionGadgetsLocalize.malformedRegularExpressionDisplayName().get();
}

@Override
@Nonnull
public String buildErrorString(Object... infos) {
return infos.length == 0
? InspectionGadgetsLocalize.malformedRegularExpressionProblemDescriptor1().get()
: InspectionGadgetsLocalize.malformedRegularExpressionProblemDescriptor2(infos[0]).get();
}
@Override
@Nonnull
public String buildErrorString(Object... infos) {
return infos.length == 0
? InspectionGadgetsLocalize.malformedRegularExpressionProblemDescriptor1().get()
: InspectionGadgetsLocalize.malformedRegularExpressionProblemDescriptor2(infos[0]).get();
}

@Override
public boolean isEnabledByDefault() {
return true;
}
@Override
public boolean isEnabledByDefault() {
return true;
}

@Override
public BaseInspectionVisitor buildVisitor() {
return new MalformedRegexVisitor();
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new MalformedRegexVisitor();
}

private static class MalformedRegexVisitor extends BaseInspectionVisitor {
private static class MalformedRegexVisitor extends BaseInspectionVisitor {

@Override
public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList == null) {
return;
}
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
final PsiExpression argument = arguments[0];
if (!ExpressionUtils.hasStringType(argument)) {
return;
}
if (!PsiUtil.isConstantExpression(argument)) {
return;
}
final PsiType regexType = argument.getType();
final String value = (String)ConstantExpressionUtil.computeCastTo(argument, regexType);
if (value == null) {
return;
}
if (!MethodCallUtils.isCallToRegexMethod(expression)) {
return;
}
//noinspection UnusedCatchParameter,ProhibitedExceptionCaught
try {
Pattern.compile(value);
}
catch (PatternSyntaxException e) {
registerError(argument, e.getDescription());
}
catch (NullPointerException e) {
registerError(argument); // due to a bug in the sun regex code
}
@Override
public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList == null) {
return;
}
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
final PsiExpression argument = arguments[0];
if (!ExpressionUtils.hasStringType(argument)) {
return;
}
if (!PsiUtil.isConstantExpression(argument)) {
return;
}
final PsiType regexType = argument.getType();
final String value = (String) ConstantExpressionUtil.computeCastTo(argument, regexType);
if (value == null) {
return;
}
if (!MethodCallUtils.isCallToRegexMethod(expression)) {
return;
}
//noinspection UnusedCatchParameter,ProhibitedExceptionCaught
try {
Pattern.compile(value);
}
catch (PatternSyntaxException e) {
registerError(argument, e.getDescription());
}
catch (NullPointerException e) {
registerError(argument); // due to a bug in the sun regex code
}
}
}
}
}
Loading
Loading