Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
How do I get rid of this 'warning: the expression is unused' warning?

Trying to figure out why I'm getting a warning in this code.

object Test {
  val bar = true

  operator fun invoke(foo: Test.() -> Boolean): Boolean {
    return foo()
  }
}

fun main(args: Array<String>) {
  println(Test {bar})
}

You get this when you compile

test.kt:5:12: warning: the expression is unused
return foo()

But it certainly seems to be used, since you do get true when you run it.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Calling foo() explicitely on this will make the warning go away:

1st way:

operator fun invoke(foo: Test.() -> Boolean): Boolean {
    return this.foo()
}

2nd way:

operator fun invoke(foo: Test.() -> Boolean): Boolean {
    return foo(this)
}

Since you should be able to omit this in this context, I would guess that it is a bug in the compiler.


If you just want to warning to disappear, you can use the @Suppress annotation at the statement level:

operator fun invoke(foo: Test.() -> Boolean): Boolean {
    @Suppress("UNUSED_EXPRESSION")
    return foo()
}

You could do it for the whole file, the whole class or the whole function as well, but it is better to use it directly at the statement because other unused expressions could really be a mistake of yours (and not by intention) and the compiler wouldn't tell you about it then.

over 4 years ago · Santiago Trujillo Relatório

0

This false positive you've identified is a known issue in Kotlin KT-21282, so you can either apply one of Willi's solutions or wait for JetBrains to fix the bug.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda