Commit Graph

43 Commits

Author SHA1 Message Date
Bas Leijdekkers
0b1872f794 java: report string.toCharArray().length -> string.length() (IDEA-351174)
for "Redundant 'String' operation" inspection

GitOrigin-RevId: 96c3b5d310b9639c74241f48348257bbb83dbe93
2024-04-11 09:52:00 +00:00
Bas Leijdekkers
e3ccfd0e4a [java] Report even more unnecessary substring() calls (IDEA-345335)
GitOrigin-RevId: 37b86167b5d2b1cac74365eaadbf3a5ddf792f69
2024-03-18 00:08:54 +00:00
Fabrice Tiercelin
94c43389e6 [java-inspections] OrderedBinaryExpression; used in SizeReplaceableByIsEmptyInspection
GitOrigin-RevId: 1fedec592b56b558cd3c6e6ba4348fed7284e5ca
2023-06-05 16:00:36 +00:00
Andrey Cherkasov
3ec4cf4a76 RedundantStringOperationInspection: String constructor can be simplified in case the passed array copied entirely
IDEA-226130

GitOrigin-RevId: d20197f50aeceaea2074e905b94ea7d838d53b4a
2022-12-22 15:59:30 +00:00
Andrey Cherkasov
f0efafd51c [java-inspections] RedundantStringOperation: suggest using 'String#contentEquals' to compare strings with StringBuilder
IDEA-273162

GitOrigin-RevId: 89c0c2d697cec22b8d5c661f2063e35ccab9e9aa
2022-12-22 15:59:27 +00:00
Bas Leijdekkers
3d5589bde2 IG: better messages and fixed highlighting for "Redundant 'String' operation" inspection
GitOrigin-RevId: f328b47c3c64b6f078f89fa04f0353ec34d57f24
2022-08-29 22:19:49 +00:00
Bas Leijdekkers
e7c55493a1 Java: fix IOE on replacing single character string operation
GitOrigin-RevId: 26ba697294eebb109148b86672a22e7aa2e921e4
2022-08-29 22:19:46 +00:00
Tagir Valeev
7e1064ea3c [java-intentions] More preview tests; minor fixes
GitOrigin-RevId: 22a46c15d8900d8a31514846755a013f6a67ad42
2022-07-29 17:55:13 +00:00
Andrey.Cherkasov
8cebcabdc1 [java-inspections] RedundantStringOperation: suggest a more suitable quick-fix
IDEA-296617

GitOrigin-RevId: 03e7b3d6c733e8630e29a5867dc715daf5c1d2a9
2022-06-24 02:20:54 +00:00
Bas Leijdekkers
ed2c73e98c more consistent Java inspection names
GitOrigin-RevId: 5c958dfd8b5f55e9e5bede82be5015c3299dd16e
2021-12-20 10:55:14 +00:00
Andrey.Cherkasov
a71e14445a [java-inspection] RedundantStringOperationInspection: fix test
GitOrigin-RevId: 7e3d6b028b954284046502313e425b8cedf0b37e
2021-09-15 15:03:36 +00:00
Andrey.Cherkasov
8964b50053 [java-inspection] RedundantStringOperation: String.strip.isEmpty() can be replaced with String.isBlank()
GitOrigin-RevId: 04b4a572ac5b1674aaf87d7145cc82a5c98772dc
2021-09-13 19:01:04 +00:00
Tagir Valeev
c0571a10d5 [java-inspections] IDEA-275886 IntelliJ is proposing wrong usage of equalsIgnoreCase refactoring
GitOrigin-RevId: 2f7e2091e31c781e42908c9fb0b849f56e50065c
2021-08-12 11:46:24 +00:00
Andrey.Cherkasov
50f8045484 [java-inspections] Redundant char[] wrapping at String constructor could be replaced with String.valueOf() IDEA-262555
GitOrigin-RevId: cae0690cf100730f6a8502b17b57d7bb913a0cf0
2021-05-17 14:25:52 +00:00
Andrey.Cherkasov
da3be3afc7 [java-inspection] IDEA-265275 Report s.substring(s.length())
GitOrigin-RevId: 6d4ad2a3ac2586707e61a166ee62c3b5bee59b76
2021-04-23 03:12:33 +03:00
Andrey.Cherkasov
50af51d8d1 Support the case when then array initializer is taken into parentheses; more attention to comments in fix: IDEA-238645
GitOrigin-RevId: d4557daa9b37e9f103b6453d9e0ad3a18df0a870
2020-09-16 12:30:36 +00:00
Andrey.Cherkasov
d6ecf8f1f0 Fixes after review: IDEA-249165
GitOrigin-RevId: 5f90b50bd298dbc73283a6b2e78267483211c111
2020-09-15 21:23:00 +00:00
Andrey.Cherkasov
6100adc63f IDEA-238645 Test fixes and some refactoring
GitOrigin-RevId: 5a3ac649c4639a0a2981608c1f503fbea72ce3be
2020-09-15 01:56:30 +00:00
pyltsin
d808337407 IDEA-217572 Inspection to replace String.toLowerCase().equals() with String.equalsIgnoreCase()
Contributed by pyltsin <pyltsin-m@yandex.ru> (PR#1398)

GitOrigin-RevId: 8f8582ce822deb0dadb6ba89834e39f3b5db3aa6
2020-07-06 05:49:10 +00:00
Nikita Eshkeev
ce7dae26b1 [codeInsight] IDEA-240288 Inspection for StringBuilder.toString().substring()
This patch replaces the excessive extraction of a qualifier via `ExpressionUtils.getEffectiveQualifier` with a simple `call.getMethodExpression().getQualifierExpression`

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: 4a4f1c6b218c63691fa2bd4610e9901874d3c712
2020-05-15 12:15:31 +00:00
Nikita Eshkeev
0218cec2c9 [codeInsight] IDEA-240288 Inspection for StringBuilder.toString().substring()
This patch removes the detection of redundant calls to `StringBuilder#toString` from the `RedundantStringOperationInspection` since there is a more general inspection called `UnnecessaryToStringCallInspection` which handles all the redundant `Object#toString` calls. All the related tests were moved to the test data of `UnnecessaryToStringCallInspectionTest`

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: 1fe7723fa4369b797120c5bbc6a6b24947c84a94
2020-05-14 22:04:54 +00:00
Nikita Eshkeev
ae75412782 [codeInsight] IDEA-240288 Inspection for StringBuilder.toString().substring()
This patch fixes the syntax error in testData

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: 90aae74914c4143dcb1ebff9e7d0c585f373718b
2020-05-14 15:42:52 +00:00
Nikita Eshkeev
06fbc3bdd2 [codeInsight] IDEA-240288 Inspection for StringBuilder.toString().substring()
This patch simplifies the `RedundantStringOperationInspection#isOperandOfStringType` method by checking if the type of the passed operand is java.lang.String. The check is the same for any passed operand since they are of the `PsiExpression` type and hence poses the `PsiExpression#getType` method.

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: bcd489d41d1272877a1bbf6032ef837a228a18e1
2020-05-13 00:55:37 +00:00
Nikita Eshkeev
cb47b00f60 [codeInsight] IDEA-240288 Inspection for StringBuilder.toString().substring()
This patch alters the `getRedundantStringBuilderToStringProblem` method with more sophisticated algorithm to detect unnecessary freestanding `StringBuilder#toString` calls. If the call is a part of a polyadic expression it checks if there is at least one string operand (either a constant literal or a local variable or a constant variable or a method call) and based on that information it detects redundant `StringBuilder#toString` calls.

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: e09ea8b8829ad9e28e8ff1bcfad34cf6b191aa6b
2020-05-12 00:59:21 +00:00
Nikita Eshkeev
597a92c0b2 [codeInsight] IDEA-240288 Inspection for StringBuilder.toString().substring()
This patch enhances RedundantStringOperationInspection with detecting either unnecessary naked calls StringBuilder.toString or redundant toString in StringBuilder.toString.substring.

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: 3cd7e964c0acc73fb1ac38061ef31264035f82dc
2020-05-09 05:25:10 +00:00
Nikita Eshkeev
565443bca4 [codeInsight] IDEA-219640 Inspection to detect some pointless String.substring
This patch fixes the notes from the review, it contains:

1. fixing the javadoc for ExpressionUtils#isConversionToStringNecessary
2. properly handling JavaTokenType.MINUS in ExpressionUtils#isDifference
3. properly handling complex PsiPolyadicExpression values in ExpressionUtils#isDifference
4. changing ProblemHighlightType to GENERIC_ERROR_OR_WARNING for
inspecitons like these
5. fixing the comments duplication problem when replacing the old string
with a new one for inspections like stringValue.substring(0, 1).equals("_")

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: 907524c9026e2d1d7cd6f0bd7b988e478e73298d
2020-04-20 20:10:15 +00:00
Nikita Eshkeev
0aa2e160b0 [codeInsight] IDEA-219640 Inspection to detect some pointless String.substring
This patch fixes the notes from the review, it includes:

- restoring annotation.naming.convention.display.name
- using com.intellij.openapi.util.Pair instead of custom private objects
- using EquivalenceChecker.expressionsMatch in order to extract
difference between two PsiBinaryExpression
- using BoolUtils.findNegation to properly deduce the sign of the equals
clause
- restoring comments while replacing substring with charAt
- properly handling special characters (e.g. '\\', '\n', '\"', etc.) in
equals clause when it is converted to charAt + "=="
- eliminating the imperative form of a message for the inspection of
substring + equals

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: be1dfd53a6af10979485cd172af39653faafb744
2020-04-17 22:57:24 +00:00
Nikita Eshkeev
7b6e636c13 [codeInsight] IDEA-219640 Inspection to detect some pointless String.substring
IDEA didn't use to report pointless String.substring invocations for
cases when only one character in the string is extracted. In such cases
it makes more sense to use the String#charAt method because it is
allocation free.

IDEA didn't use to suggest to refactor for the code when String.substring
selects a single character and is followed by the equals method(e.g.
args[0].substring(0, 1).equals("_")) to more readable version e.g.
args[0].charAt(0) == '_'.

This patch adds such refactorings to both of the cases. It also adds
the possiblity to handle expressions like "i+1" and "i+2" to
com.siyeh.ig.psiutils.ExpressionUtils#isDifference.

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: 6d179c7c9c605f08b4d4468712f8468fd68045a1
2020-04-15 21:31:10 +00:00
Tagir Valeev
98792cbf73 Revert substring(x).indexOf(y) -> indexOf(y, x) (IDEA-219640, case#3); was incorrect replacement
GitOrigin-RevId: f62ccdd76bd6353fe1bce95fe17ab4b3ca507dce
2019-11-25 09:31:23 +00:00
Tagir Valeev
25dc0f385c s.substring(s.length()-fooLen).equals(foo) -> s.endsWith(foo)
GitOrigin-RevId: 5dbd78e6359770f9680ef25dcf699297b5a28e46
2019-11-24 02:05:56 +00:00
Tagir Valeev
476b3ff5bb substring(x, x+y.length()).equals(y) -> startsWith(y, x) (IDEA-226148, case#1)
GitOrigin-RevId: 04fe1be3e9f48783ad8d95897497ea55109453f5
2019-11-22 06:36:34 +00:00
Tagir Valeev
7f8d9a2372 substring(x).indexOf(y) -> indexOf(y, x) (IDEA-219640, case#3)
GitOrigin-RevId: aab34215f1dc50e33af12cc56954dc17b9104318
2019-11-22 06:36:34 +00:00
Tagir Valeev
50b74c2848 IDEA-225861 Detect a case related to String transformation where result can be predicted before transformation
GitOrigin-RevId: 3448b86219bebc4914cccae89e7e46db67d81128
2019-11-07 02:40:58 +00:00
Tagir Valeev
11e73d577f RedundantStringOperation: id changed, merger updated (IDEA-CR-34351) 2018-11-04 10:54:34 +07:00
Tagir Valeev
e616b88fa7 IDEA-194396 Merge StringConstructorInspection into RedundantStringOperationInspection 2018-07-02 17:50:00 +07:00
Tagir Valeev
4861c7cc17 RedundantStringOperation: refactored to CallMapper; indexOf/startsWith/lastIndexOf scenarios
Fixes IDEA-191536 More redundant string operations with indexOf lastIndexOf startsWith
2018-05-10 12:01:59 +07:00
Tagir Valeev
f6c45995d3 IDEA-187224: test fixed (beforeStringSubstringLength.java was not added mistakenly) 2018-02-28 14:15:23 +07:00
Tagir Valeev
5feb5cb75a IDEA-187224 Add inspection for substring with String#length as second param 2018-02-27 13:05:45 +07:00
Tagir Valeev
2f547ca38f RedundantStringOperationInspection: warn about append(s.substring(x, y))
Replaceable with append(s, x, y)
Fixes IDEA-185652 Improve StringBulder.append(s.substring(1,2)) intention
Also optimization of new expression resolution
2018-01-29 13:34:55 +07:00
Tagir Valeev
30b3d4a558 IDEA-184825 "Redundant String operation" inspection - rename 'Delete element' to 'Delete argument' 2018-01-12 11:44:40 +07:00
Tagir Valeev
e4efb836cd RedundantStringOperationInspection: fixes after review IDEA-CR-25339
1. messages cleanup
2. new StringBuilder(""). PrintStream.println("") are warned
2017-10-17 10:58:07 +07:00
Tagir Valeev
31b328cdca RedundantStringOperation: append("") (IDEA-180178), intern()
ConstantStringInternInspection removed
2017-10-11 14:35:28 +07:00
Tagir Valeev
454efb3280 RedundantStringOperationInspection: SubstringZero and StringToString merged 2017-10-11 14:35:27 +07:00