kotlin-dsl-samples icon indicating copy to clipboard operation
kotlin-dsl-samples copied to clipboard

IntelliJ flags green instruction as red with Copy task filter method

Open jnizet opened this issue 7 years ago • 3 comments

Expected Behavior

I expect IntelliJ not to flag the code as incorrect

Current Behavior

The IntelliJ editor flags the code as incorrect (see below for details)

Context

I was translating the "working with files" example of the gradle user guide when I noticed this. No clear idea if it's a DSL or an IntelliJ bug, but Eric Wendelin asked me to submit an issue here, so here it goes. Feel free to open an issue for IntelliJ if necessary.

Steps to Reproduce (for bugs)

  • Install Gradle 4.10-rc-3 (using the wrapper)
  • Create a gradle build script in Kotlin with the following task:
tasks.create<Copy>("bizarreIdeaBug") {
    from("a")
    into("b")
    filter { line: String ->
        if (line.startsWith("-")) null else line
    }
}

Open it in IntelliJ. Here's my version: IntelliJ IDEA 2018.2.2 (Ultimate Edition) Build #IU-182.4129.33, built on August 21, 2018 JRE: 1.8.0_152-release-1248-b8 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o macOS 10.13.6

executing the task works fine. Importing the project in IntelliJ works fine, but the editor complains, with a red squiggly line under null, with a message saying "Null can not be a value of a non-null type String"

Your Environment

------------------------------------------------------------
Gradle 4.10-rc-3
------------------------------------------------------------

Build time:   2018-08-23 12:07:56 UTC
Revision:     1b827f2119c11123e178e731421ae79d52a8991a

Kotlin DSL:   1.0-rc-3
Kotlin:       1.2.60
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_152 (Oracle Corporation 25.152-b16)
OS:           Mac OS X 10.13.6 x86_64

jnizet avatar Aug 24 '18 21:08 jnizet

I can confirm something is going on.

Using IntelliJ IDEA 2018.2.3 with the Kotlin plugin v1.2.70 I get:

image

Which is different from what you got with 2018.2.2 (and what version of the Kotlin plugin?). Other behavior is the same: compiles and runs fine from the command line.

Intellij can be made happy by casting lying:

image

But it doesn't make sense.

It is as if IntelliJ interprets CopySpec filter(Transformer<String, String> transformer); with JSR-305 strict mode differently than the Kotlin compiler.

Given the return type is a type parameter it should be String!, allowing null.

eskatos avatar Sep 18 '18 12:09 eskatos

null as String will fail to cast (at runtime) I think. Does null as? String work or does it give the same error as @jnizet was experiencing?

JLLeitschuh avatar Sep 19 '18 15:09 JLLeitschuh

@JLLeitschuh, yes, same error

@jnizet, that's a bug in IntelliJ, could you please open a http://kotl.in/issue?

eskatos avatar Nov 06 '18 14:11 eskatos