Skip to content
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

Unclear Error Message When Inheriting Generic Class #2299

Closed
patrickschmidt-tr opened this issue Oct 11, 2023 · 2 comments · Fixed by #2302 or #2303
Closed

Unclear Error Message When Inheriting Generic Class #2299

patrickschmidt-tr opened this issue Oct 11, 2023 · 2 comments · Fixed by #2302 or #2303

Comments

@patrickschmidt-tr
Copy link

Expected Behavior

When inheriting a generic class, I would expect clearer error messages regarding the formatting as it's not clear to me what to change to get it accepted by the linter. I would also expect that the auto formatter would be able to correct the problem.

Observed Behavior

here is the output of ktlint -l debug:

lint

10:42:24.670 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintServiceLoader - Discovered RuleSetProviderV3 with id 'standard' in ktlint JAR
10:42:24.787 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintServiceLoader - Discovered ReporterProviderV2 with id 'baseline' in ktlint JAR
10:42:24.787 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintServiceLoader - Discovered ReporterProviderV2 with id 'plain' in ktlint JAR
10:42:24.787 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintServiceLoader - Discovered ReporterProviderV2 with id 'checkstyle' in ktlint JAR
10:42:24.787 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintServiceLoader - Discovered ReporterProviderV2 with id 'json' in ktlint JAR
10:42:24.787 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintServiceLoader - Discovered ReporterProviderV2 with id 'format' in ktlint JAR
10:42:24.787 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintServiceLoader - Discovered ReporterProviderV2 with id 'html' in ktlint JAR
10:42:24.787 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintServiceLoader - Discovered ReporterProviderV2 with id 'plain-summary' in ktlint JAR
10:42:24.787 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintServiceLoader - Discovered ReporterProviderV2 with id 'sarif' in ktlint JAR
10:42:24.788 [main] DEBUG com.pinterest.ktlint.cli.internal.ReporterAggregator - Initializing "plain" reporter with {plain=true, color=false, color_name=DARK_GRAY, format=false}
10:42:24.790 [main] DEBUG com.pinterest.ktlint.cli.internal.FileUtils - Start walkFileTree from directory: 'my directory'
10:42:24.824 [main] DEBUG com.pinterest.ktlint.cli.internal.FileUtils - Discovered 1 files to be processed in 34 ms
10:42:24.832 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine - Starting with linting file 'Example.kt'
10:42:25.019 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.internal.EditorConfigLoader - Effective editorconfig properties for file 'Example.kt':
	ij_kotlin_allow_trailing_comma_on_call_site: true
	ij_kotlin_allow_trailing_comma: true
	ktlint_standard_annotation: disabled
	ktlint_standard_annotation-spacing: disabled
	ktlint_standard_multiline-if-else: disabled
10:42:25.025 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.internal.rulefilter.RunAfterRuleFilter - Rule with id 'standard:argument-list-wrapping' should run after the rule with id 'standard:class-signature'. However, the latter rule is not loaded and is allowed to be ignored. For best results, it is advised load the rule.
10:42:25.025 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.internal.rulefilter.RunAfterRuleFilter - Rule with id 'standard:indent' should run after the rule with id 'standard:class-signature'. However, the latter rule is not loaded and is allowed to be ignored. For best results, it is advised load the rule.
10:42:25.025 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.internal.rulefilter.RunAfterRuleFilter - Rule with id 'standard:wrapping' should run after the rule with id 'standard:annotation'. However, the latter rule is not loaded and is allowed to be ignored. For best results, it is advised load the rule.
10:42:25.026 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.internal.rulefilter.RunAfterRuleFilter - Rule with id 'standard:argument-list-wrapping' should run after the rule with id 'standard:class-signature'. However, the latter rule is not loaded and is allowed to be ignored. For best results, it is advised load the rule.
10:42:25.026 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.internal.rulefilter.RunAfterRuleFilter - Rule with id 'standard:indent' should run after the rule with id 'standard:class-signature'. However, the latter rule is not loaded and is allowed to be ignored. For best results, it is advised load the rule.
10:42:25.026 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.internal.rulefilter.RunAfterRuleFilter - Rule with id 'standard:indent' should run after the rule with id 'standard:class-signature'. However, the latter rule is not loaded and is allowed to be ignored. For best results, it is advised load the rule.
10:42:25.035 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.internal.RuleProviderSorter - Rules will be executed in order below:
           - internal:ktlint-suppression,
           - standard:blank-line-before-declaration,
           - standard:chain-wrapping,
           - standard:class-naming,
           - standard:colon-spacing,
           - standard:comma-spacing,
           - standard:comment-spacing,
           - standard:comment-wrapping,
           - standard:context-receiver-wrapping,
           - standard:curly-spacing,
           - standard:discouraged-comment-location,
           - standard:dot-spacing,
           - standard:double-colon-spacing,
           - standard:enum-entry-name-case,
           - standard:enum-wrapping,
           - standard:filename,
           - standard:final-newline,
           - standard:fun-keyword-spacing,
           - standard:function-naming,
           - standard:function-return-type-spacing,
           - standard:function-start-of-body-spacing,
           - standard:function-type-reference-spacing,
           - standard:if-else-bracing,
           - standard:import-ordering,
           - standard:kdoc-wrapping,
           - standard:keyword-spacing,
           - standard:modifier-list-spacing,
           - standard:modifier-order,
           - standard:multiline-expression-wrapping,
           - standard:no-blank-line-before-rbrace,
           - standard:no-blank-line-in-list,
           - standard:no-blank-lines-in-chained-method-calls,
           - standard:no-consecutive-blank-lines,
           - standard:no-consecutive-comments,
           - standard:no-empty-class-body,
           - standard:no-empty-file,
           - standard:no-empty-first-line-in-class-body,
           - standard:no-empty-first-line-in-method-block,
           - standard:no-line-break-after-else,
           - standard:no-line-break-before-assignment,
           - standard:no-multi-spaces,
           - standard:no-trailing-spaces,
           - standard:no-unit-return,
           - standard:no-unused-imports,
           - standard:no-wildcard-imports,
           - standard:nullable-type-spacing,
           - standard:op-spacing,
           - standard:package-name,
           - standard:parameter-list-spacing,
           - standard:parameter-list-wrapping,
           - standard:parameter-wrapping,
           - standard:paren-spacing,
           - standard:property-naming,
           - standard:property-wrapping,
           - standard:range-spacing,
           - standard:spacing-around-angle-brackets,
           - standard:spacing-between-declarations-with-annotations,
           - standard:spacing-between-declarations-with-comments,
           - standard:spacing-between-function-name-and-opening-parenthesis,
           - standard:statement-wrapping,
           - standard:string-template,
           - standard:try-catch-finally-spacing,
           - standard:type-argument-list-spacing,
           - standard:type-parameter-list-spacing,
           - standard:unary-op-spacing,
           - standard:unnecessary-parentheses-before-trailing-lambda,
           - standard:if-else-wrapping,
           - standard:no-single-line-block-comment,
           - standard:wrapping,
           - standard:argument-list-wrapping,
           - standard:no-semi,
           - standard:function-signature,
           - standard:trailing-comma-on-call-site,
           - standard:trailing-comma-on-declaration-site,
           - standard:indent,
           - standard:block-comment-initial-star-alignment,
           - standard:string-template-indent,
           - standard:max-line-length
10:42:25.068 [pool-1-thread-1] DEBUG com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine - Finished with linting file 'Example.kt'
Example.kt:13:13: Expected newline (standard:type-argument-list-spacing)
/Example.kt:17:13: Expected newline (standard:type-argument-list-spacing)
10:42:25.068 [main] WARN com.pinterest.ktlint.cli.internal.KtlintCommandLine - Lint has found errors than can be autocorrected using 'ktlint --format'

Summary error count (descending) by rule:
  standard:type-argument-list-spacing: 2
10:42:25.069 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintCommandLine - Finished processing in 404ms / 1 file(s) scanned / 2 error(s) found
10:42:25.069 [main] DEBUG com.pinterest.ktlint.cli.internal.KtlintCommandLine - Exit ktlint with exit code: 1

Running ktlint -F produces similar output but also says:

10:51:12.630 [pool-1-thread-1] WARN com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine - Format was not able to resolve all violations which (theoretically) can be autocorrected in file Example.kt in 3 consecutive runs of format.

Steps to Reproduce

open class Example<T, U, V, W>(
    val a: T,
    val b: U,
    val c: V,
    val d: W,
)

class InheritingExample(
    a: String,
    b: Int,
) : Example<
        String,
        Int,
        String,
        Int,
        >(
        a,
        b,
        "c",
        1,
    )

and then just run ktlint on that file

Your Environment

  • Version of ktlint used: 1.0.0
  • Relevant parts of the .editorconfig settings
[*.{kt,kts}]
ij_kotlin_allow_trailing_comma_on_call_site=true
ij_kotlin_allow_trailing_comma=true
ktlint_standard_annotation=disabled
ktlint_standard_annotation-spacing=disabled
ktlint_standard_multiline-if-else=disabled
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): just running ktlint is enough
  • Version of Gradle used (if applicable):
  • Operating System and version: macOS 12.6.8
@paul-dingemans
Copy link
Collaborator

10:51:12.630 [pool-1-thread-1] WARN com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine - Format was not able to resolve all violations which (theoretically) can be autocorrected in file Example.kt in 3 consecutive runs of format.

This typically happens when multiple rules are 'fighting' each other and are negating the changes of each other. So when you try to do the changes manually, it gets very confusing as it does not resolve the problem.

In this case it seems that the class-signature rule want to format the class as follows:

class InheritingExample(
    a: String,
    b: Int,
) : Example<
    String,
        Int,
        String,
        Int,
    >(
        a,
        b,
        "c",
        1,
    )

while the indent rule wants it to be formatted as:

class InheritingExample(
    a: String,
    b: Int,
) : Example<
        String,
        Int,
        String,
        Int,
        >(
        a,
        b,
        "c",
        1,
    )

As the indent rules always runs later than the class-signature rule, it negates the change of the class-signature rule.

The violations reported by the type-argument-list-spacing rule can be ignored for this example as it overlaps with the class-signature rule. Clarifying the violation message won't help. The indent rule needs fixing so that it aligns with the class-signature rule.

@paul-dingemans paul-dingemans added this to the 1.0.1 milestone Oct 11, 2023
paul-dingemans added a commit that referenced this issue Oct 12, 2023
…e parameter lists in ktlint_official code style

This makes indentation of parenthesis, brackets and angle brackets consistent. E.g. in multiline format the elements of the list (excluding the closing token) are indented one indent level deep. The closing token is kept at the same indent level as the opening token.

Closes #2299
@paul-dingemans paul-dingemans modified the milestones: 1.0.1, 1.1 Oct 12, 2023
paul-dingemans added a commit that referenced this issue Oct 12, 2023
@paul-dingemans paul-dingemans modified the milestones: 1.1, 1.0.1 Oct 12, 2023
@paul-dingemans
Copy link
Collaborator

In 1.0.1 a fix will be released that fixes the false positives violations about adding a newline (PR #2303). In 1.1.0 the conflict between the indent and class-signature rule will be released as indent rule will functionally be changed (PR #2302).

paul-dingemans added a commit that referenced this issue Oct 13, 2023
paul-dingemans added a commit that referenced this issue Oct 15, 2023
…e parameter lists in ktlint_official code style (#2302)

This makes indentation of parenthesis, brackets and angle brackets consistent. E.g. in multiline format the elements of the list (excluding the closing token) are indented one indent level deep. The closing token is kept at the same indent level as the opening token.

Closes #2299
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment