236 Commits

Author SHA1 Message Date
Dominic Zirbel
1e696fd9fc Add MaxChainedCallsOnSameLine style rule (#4985)
Add a new rule MaxChainedCallsOnSameLine to limit the number of chained calls on placed on a single line. This works well alongside CascadingCallWrapping in #4979 to make long call chains more readable by wrapping them on new lines.
2022-06-28 15:28:01 +02:00
Dominic Zirbel
c8eb24c04b UnnecessaryApply: fix false negative for assignment (#4948)
Fix a missed case of UnnecessaryApply where the apply contains a single field assignment but the result of the apply{} is unused. This pattern is actually used in 2/3 of the <noncompliant> examples and while it occurred in tests for false positives, no test actually checked that warnings were generated in the simple case.
2022-06-27 17:09:25 +02:00
Vitaly V. Pinchuk
47428426d0 Fix false-negative for CanBeNonNullable (#4993)
* Report null-check returning 'Unit'

The rule will report code smell when null-check expression returns 'Unit'

* Not report guard statement with side effect ahead
2022-06-24 17:09:31 +02:00
Dominic Zirbel
89f6ec1586 Add CascadingCallWrapping style rule (#4979)
Add a new rule CascadingCallWrapping which requires that if a chained call is placed on a newline then all subsequent calls must be as well, improving readability of long chains.
2022-06-22 18:18:52 +02:00
Matthew Haughton
b06276af54 Fix rule code samples to be valid Kotlin code (#4969) 2022-06-19 02:05:29 +02:00
Toshiaki Kameyama
0ecebd4f61 UnusedPrivateMember: highlight declaration name (#4928) 2022-06-07 21:14:51 +02:00
Gabriel Freitas Vasconcelos
5baf1f6955 Creates ForbiddenSuppress rule (#4899)
This rule is based on discussion #4890.

Checks for suppression of one or more rules that have been decided to
never be suppressed within a code base. For example, if we've set
MaximumLineLength to a value, every file must be compliant, and if there
is a special case where compliance is discouraged, we can simply exclude
this file within YML configuration.

This rule cannot prevent itself from being suppressed, but should serve
to discourage the over use of `@Suppress` annotations.
2022-06-03 17:19:23 +01:00
marschwar
23a03e190d Allow additionalJavaSourceRootPaths to be defined on @KotlinCoreEnvironmentTest (#4771)
* Allow additionalJavaSourceRootPaths to be defined on @KotlinCoreEnvironmentTest

* update api

* Rename top level nested classes to WithDefaultSources and WithAdditionalJavaSources

* fix compilation error in java < 11

* fix upstream merge

* fix: do not attempt to compile snippet with java class from resource

* manually fix indentation of test snippets

Co-authored-by: Markus Schwarz <post@markus-schwarz.net>
2022-06-02 00:07:49 +02:00
schalkms
8b365028ec Remove redundant character escape in RegExp (#4892) 2022-06-01 23:08:27 +02:00
Brais Gabín
6d2933dc42 Active rules by default (#4875) 2022-06-01 23:02:12 +02:00
Dominic Zirbel
db0b4b5b06 UselessCallOnNotNull: fix false positive for unresolved types (#4880)
Fix an issue where UselessCallOnNotNull would report issues when some of the argument types resolved as ErrorType. While it correctly handles cases where the BindingContext cannot resolve a type (the first test case), in cases where a call is made on an unknown type (the second test case, e.g. via takeIf{}) the BindingContext will report it as an ErrorType. Since ErrorType.isNullable() apparently returns false we need to make an exception to ensure the rule behaves as expected.
2022-06-01 20:03:45 +02:00
Toshiaki Kameyama
05cd79612f UnnecessaryInnerClass: fix false negative with this references (#4884) 2022-06-01 18:55:51 +02:00
Dominic Zirbel
d958fbf5f5 CanBeNonNullable: fix false positives for parameterized types (#4870)
CanBeNonNullable reports that properties of parameterized types can be marked as non-nullable even when this is impossible, just because the parameterized type is itself nullable (i.e. does not inherit from a non-nullable type). Instead, the rule should only report cases where a property is explicitly marked nullable but does not need to be.
2022-05-30 22:26:55 +02:00
Dominic Zirbel
233b4a686c Add NullableBooleanCheck rule (#4872)
Add a new rule which enforces the recommendation of the Kotlin coding conventions to prefer `==` over `?:` when converting a `Boolean?` into a `Boolean`: https://kotlinlang.org/docs/coding-conventions.html#nullable-boolean-values-in-conditions. This is simple to implement and enforces consistency for the two essentially equivalent methods of coalescing nullable boolean values.

The coding conventions only specify this preference for usages "in a conditional statement" but the same rationale applies to any statement which converts a Boolean? to a Boolean, so I have implemented it for any situation. An alternative is to add a configuration setting to toggle application only in if statements.

Unfortunately, this rule requires type resolution since there are technically valid usages of e.g. `value ?: true` for cases where `value` is not a `Boolean?` but an `Any?`. Running without type resolution will not be able to distinguish between these valid usages and ones which could be converted to `== false`.
2022-05-30 10:33:13 +02:00
Dominic Zirbel
2f5b8f5933 UnnecessaryInnerClass: fix false positives labeled expression to outer class (#4865)
* UnnecessaryInnerClass: fix false positives labeled expression to outer class

Fix an issue where inner classes which reference their outer class via a labeled
expression would still be reported as unnecessary. This seems to be due to a
missing visitExpressionWithLabel() check which resolves the label name: if the
label is referencing a parent class, then the nested class must be `inner`.

I wasn't able to find a way to resolve the labeled expression as a ClassId to
match the existing class check from KtReferenceExpression, so I settled for
using the class name instead, which is easy to obtain. This means there's a fair
bit of duplication in overriden checkForOuterUsage() methods to check parent
classes which could probably be removed, but I didn't see a particularly clean
way to do so.

* UnnecessaryInnerClass: add test for double-nested inner classes using labeled expressions
2022-05-29 11:52:17 +02:00
Dominic Zirbel
2e2d4711c2 UnnecessaryInnerClass: add test for safe qualified expressions (#4864)
Add a unit test for a bug in which safe qualified references to a nullable field
in the parent class (i.e. as `?.`) would not be marked as warnings in release
1.20.0. This issue has been inadvertently fixed by
https://github.com/detekt/detekt/pull/4738, but adding a unit test will guard
against future regressions.

I was able to confirm this test fails by checking out the 1.20.0 tag and running
the unit test; against that tag the fix was to override the
visitSafeQualifiedExpression() function with the same logic as
visitCallExpression().
2022-05-27 09:16:34 +02:00
Toshiaki Kameyama
5f491f1da6 ExplicitCollectionElementAccessMethod: fix false positive for get operators with type parameters (#4803) 2022-05-21 11:43:18 +02:00
Brais Gabín
5af71841d7 Improve issue description and smell message of DestructuringDeclarationWithTooManyEntries (#4795) 2022-05-12 13:00:11 +02:00
Marie Katrine Ekeberg
b1e223239b Fix ValShouldBeVar false positive inside unknown type (#4820)
* Fix ValShouldBeVar false positive inside unknown type

* Added testcase for VarShouldBeVar positives when inside known context
2022-05-09 17:58:47 +02:00
Marie Katrine Ekeberg
463fbf19a5 False positive for unused imports #4815 (#4818)
* Handle all kdoc sections in UnusedImports, not just the first one

* Fix NoNameShadowing and UnnecessaryLet violations
2022-05-08 17:39:28 -07:00
Toshiaki Kameyama
da0c92417c UseOrEmpty: fix false positive for indexing operator calls with type parameters (#4804)
* UseOrEmpty: fix false positive for indexing operator calls with type parameters

* Fix test
2022-05-05 09:31:27 +02:00
Toshiaki Kameyama
61cfc24cd6 UnnecessaryAbstractClass: report only the class name (#4808) 2022-05-04 18:26:29 +02:00
Marie Katrine Ekeberg
30af7dc16e Fix wrong replacement suggestion for UnnecessaryFilter (#4807) 2022-05-03 23:13:42 +02:00
Brais Gabín
a2734b18c1 Remove Unnecesary @Nested (#4740) 2022-04-25 23:04:26 +02:00
Toshiaki Kameyama
f44947c5c6 Add UnnecessaryBackticks rule (#4764) 2022-04-25 22:31:30 +02:00
marschwar
1d6beb998e fix: add test case that fails if environment is not properly set up (#4769)
* fix: add test case that fails if environment is not properly set up

* add reasoning for additional test case

Co-authored-by: Markus Schwarz <post@markus-schwarz.net>
2022-04-25 22:04:11 +02:00
Richard Li
1e506d3a37 VarCouldBeVal: Add configuration flag ignoreLateinitVar (#4745) 2022-04-24 12:51:15 +01:00
Toshiaki Kameyama
0695d7e5e0 UnnecessaryInnerClass: fix false positive with references to function type variables (#4738) 2022-04-20 08:54:54 +02:00
Toshiaki Kameyama
42e4d0aa6e Fix false positive on VarCouldBeVal in generic classes (#4733) 2022-04-19 09:03:24 +02:00
Toshiaki Kameyama
683e696375 OptionalWhenBraces: fix false negative when the single statement has comments inside (#4722) 2022-04-17 16:59:04 +02:00
Vitaly V. Pinchuk
471764cb85 Make links clickable in rule classes documentation (#4713)
Co-authored-by: M Schalk <30376729+schalkms@users.noreply.github.com>
2022-04-14 10:52:52 +02:00
renovate[bot]
ed8b0890c3 Update org.jetbrains.kotlin to v1.6.20 (#4665)
* Update org.jetbrains.kotlin to v1.6.20

* Replace non-exhaustive when with boolean expression

* Replace deprecated imports

* Use -opt-in instead of deprecated -Xopt-in

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Matthew Haughton <3flex@users.noreply.github.com>
2022-04-07 08:54:51 +10:00
Maxime Meriouma-Caron
50f5f5f0ad [VarCouldBeVal] fix overrides false positives (#4664) 2022-04-06 19:21:39 +02:00
Brais Gabín
66e32e53c7 CanBeNonNullable shouldn't consider abstract properties (#4686) 2022-04-05 21:21:12 +02:00
Matthew Haughton
0be7775190 Migrate detekt-rules-style tests to JUnit (#4575)
* Migrate detekt-rules-style tests to JUnit

* Replace Spek in test cases with MockK

* Revert inadvertent changes

* Update detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt

Co-authored-by: marschwar <marschwar@users.noreply.github.com>

* Update detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt

Co-authored-by: marschwar <marschwar@users.noreply.github.com>

* Update detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt

Co-authored-by: marschwar <marschwar@users.noreply.github.com>

Co-authored-by: Chao Zhang <zhangchao6865@gmail.com>
Co-authored-by: marschwar <marschwar@users.noreply.github.com>
2022-04-05 20:01:28 +02:00
Matthew Haughton
e1c4da99b7 Fix typo (#4671) 2022-04-03 10:42:50 +02:00
Brais Gabín
e052dbe94c Function generics matcher (#4460)
* Match functions with generics

* Add test to ensure that #4448 is fixed
2022-03-31 08:48:52 +02:00
Brais Gabín
d7ac501855 Match extension functions (#4459)
* Add missing tests

* Match extension functions
2022-03-24 08:54:25 +01:00
Stieglitz
14c0f11e44 Bump KtLint to 0.44.0 and add UnnecessaryParenthesesBeforeTrailingLamda rule (#4630)
* Bump KtLint to 0.44.0 and add UnnecessaryParenthesesBeforeTrailingLambda rule

* implement new RunAsLateAsPossible and RunOnRootNodeOnly annotations

* use internal ruleShouldOnlyRunOnFileNode method

* revoke line wrap

* fix indentation errors

* deprecate indentSize in ParameterListWrapping.kt

* implement microutilsKotlinLoggingJvm and fake EDITOR_CONFIG_USER_DATA_KEY

* remove duplicate
2022-03-19 12:59:56 +01:00
Toshiaki Kameyama
7740a2a2a0 UnnecessaryAbstractClass: fix false positive when the abstract class has properties in the primary constructor (#4628)
* UnnecessaryAbstractClass: refactor

* UnnecessaryAbstractClass: fix false positive when the abstract class has properties in the primary constructor
2022-03-12 08:33:04 -08:00
Brais Gabín
dd80d61348 AnnotationSuppressor now resolves Full Qualified Annotation Names without type solving (#4570)
* Simplify AnnotationExcluderSpec

* Improve tests

* Improve tests

* Fix FullQualifiedNameGuesser

* Allow List<Regex> inside AnnotationExcluder to give more options to the callers

* Test with and without type-solving

* Make AnnotationExcluder use the BindingContext when available

* AnnotationSuppressor works with Full Qualified names without type solving

* Address PR comments

* Update detekt-psi-utils/src/test/kotlin/io/github/detekt/psi/internal/FullQualifiedNameGuesserSpec.kt

Co-authored-by: M Schalk <30376729+schalkms@users.noreply.github.com>

* Support star imports

* Improve readability

Co-authored-by: M Schalk <30376729+schalkms@users.noreply.github.com>
2022-02-24 00:03:15 +01:00
marschwar
a6a26142c5 [MaxLineLength] Fix signature in for blank characters in the Baseline (#4504) 2022-01-30 13:36:33 +00:00
severn-everett
8a39d2a172 [VarCouldBeVal] Override vars will not be flagged if bindingContext is not set (#4477) 2022-01-28 01:36:39 +00:00
Brais Gabín
576ae5f540 Match functions signatures with lambdas on it (#4458)
* Match functions signatures with lambdas on it

* Improve code
2022-01-25 09:41:04 +01:00
Brais Gabín
a6c84354e6 Improve AnnotationExcluder tests (#4368)
* Add missing deprecation

* Simplify AnnotationExcluder

* Improve tests making them more exhaustive

* Handle the globbing inside AnnotationExcluder

* Update detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/AnnotationExcluder.kt

Co-authored-by: Chao Zhang <zhangchao6865@gmail.com>

Co-authored-by: Chao Zhang <zhangchao6865@gmail.com>
2022-01-23 10:47:23 -08:00
Chao Zhang
ca9a3fa0bb Fix false positive of UnnecessaryInnerClass (#4509)
* Fix false positive of UnncessaryInnerClass

* Exclude Nested annotation because junit test requires @Nested test class to be inner

* Fix test code snippet
2022-01-23 10:17:44 -08:00
severn-everett
31520dadf2 Added rule UnnecessaryInnerClass (#4394)
* Added rule UnnecessaryInnerClass

* Addressed Detekt issues; Added test case

* Removed unnecessary code

* Addressing PR comment

* Enabled rule for self-tests on the Detekt library

* Fixing inner class issue in CognitiveComplexity

* Updated code comment
2022-01-22 17:53:01 -08:00
Vasily Maleev
bf698f01de Fix overridden function reporting for CanBeNonNullable rule (#4497) 2022-01-19 11:02:13 +01:00
Brais Gabín
116b2a1b10 Set the name of functions and paramenters between ` to improve the readability (#4488) 2022-01-15 12:41:39 +11:00
Brais Gabín
bd525fb990 Fix issue raised by detekt (#4491) 2022-01-13 19:42:21 -08:00