- the expression is unused when using kotest in intellij
- Questions : the expression is unused when using kotest in intellij
- Answers 1 : of the expression is unused when using kotest in intellij
- Answers 2 : of the expression is unused when using kotest in intellij
- A piece of code is telling me «The Expression is unused»
- Edit
- 1 Answer 1
- «the expression is unused» when using kotest in intellij
- 2 Answers 2
- Jetpack Compose Kotlin says
- 2 Answers 2
- [Solved]-How do I get rid of this ‘warning: the expression is unused’ warning?-kotlin
- Related Query
- More Query from same tag
the expression is unused when using kotest in intellij
Questions : the expression is unused when using kotest in intellij
With this kotlin test written with Kotest, help uvdos kotest IntelliJ shows the warning «The expression help uvdos kotest is unused» and syntax coloration is not help uvdos kotest working. Also, when running the tests, the help uvdos kotest test is not found.
class TalentMatchServiceSpec : StringSpec() < init < "add 3 to 2 should give 5" < // Given integers 2 and 3 val two = 2 val three = 3 // When adding both numbers val sum = two + three // Then the result is 5 sum shouldBe 5 >> >
Answers 1 : of the expression is unused when using kotest in intellij
@Thomas Martin’s answer is correct, but thread uvdos kotest you asked why it makes a difference.
The SpringSpec in Kotest relies on some thread uvdos kotest Kotlin DSL features to work. Let’s thread uvdos kotest demonstrate by building the same thread uvdos kotest function from scratch.
We would start by using Kotlin’s thread uvdos kotest extension functions. This allows us to thread uvdos kotest add functions to classes we don’t thread uvdos kotest control.
fun String.test(test: () -> Unit)
So then we can call this test function thread uvdos kotest on any string:
Secondly, Kotlin allows any final lambda thread uvdos kotest arg to be brought outside of the parens. thread uvdos kotest So foo(arg, arg2, arg3, lambda) can thread uvdos kotest become foo(arg, arg2, arg3) lambda. This thread uvdos kotest means we can write our test as:
Next, we can mark functions as infix and thread uvdos kotest then we do not need to use the dot to thread uvdos kotest invoke them. So our test function thread uvdos kotest becomes:
infix fun String.test(test: () -> Unit)
And our test now looks like:
Finally, any function named invoke and thread uvdos kotest marked as an operator function can be thread uvdos kotest invoked without the function name. So thread uvdos kotest operator fun invoke(a: String, b: thread uvdos kotest String) inside a class Foo can be thread uvdos kotest invoked as both the regular thread uvdos kotest Foo.invoke(a,b) or just Foo(a, b).
So putting all this together, our final thread uvdos kotest test function looks like:
operator fun String.invoke(test: () -> Unit)
If the braces are moved to the next thread uvdos kotest line, as in your original question, it thread uvdos kotest just looks like a String followed by an thread uvdos kotest unrelated lambda block. Two different thread uvdos kotest expressions.
Answers 2 : of the expression is unused when using kotest in intellij
Just put the opening curly bracket after thread uvdos kotest «add 3 to 2 should give 5», like this :
class TalentMatchServiceSpec : StringSpec() < init < "add 3 to 2 should give 5" < // Given integers 2 and 3 val two = 2 val three = 3 // When adding both numbers val sum = two + three // Then the result is 5 sum shouldBe 5 >> >
A piece of code is telling me «The Expression is unused»
I’m very new to App development. I’m stuck on a piece of code that’s telling me «The Expression is unused». The App itself is written in Kotlin, it’s meant to be a conversion app. I’m pretty sure I got the math right, since 1 foot comes out to 30.48cm, which is correct. But, whenever I write a number (in the app) greater than 1 foot, it still always comes to 30.48cm. An example being if I were to type 5foot 9 in the app, the answer would still be 30.48cm. Here is the two blocks of code I’m pretty sure one of them is the culprit. The first one. ‘calculateHeight’ is the line thats giving me «The Expression is unused»
private fun calculateButton() < val feetString: String = binding.editTextFeet.text.toString() val inchesString: String = binding.editTextInches.text.toString() val calculateHeight = calculateHeight() if (feetString.isEmpty()) < Toast.makeText(context, "Please select a foot value", Toast.LENGTH_SHORT).show() >else < calculateHeight displayText() >if (inchesString.isEmpty()) < Toast.makeText(context, "Please select a inch value", Toast.LENGTH_SHORT).show() >else < calculateHeight displayText() >>
private fun calculateHeight(): Double < val feetHint = binding.editTextFeet.toString() val inchesHint = binding.editTextInches.toString() var feet = 1 try < feet = feetHint.toInt() >catch (e: NumberFormatException) < e.printStackTrace() >var inches = 0f try < inches = inchesHint.toInt().toFloat() >catch (e: NumberFormatException) < e.printStackTrace() >val totalFeet = feet * 12 val totalInches = inches + 1f val heightInCentimeters = 2.54 return ((totalFeet * totalInches) * heightInCentimeters) > >
Edit
Did you mean to pass the result of calculateHeight() into displayText()? Writing calculateHeight on it’s own line doesn’t do anything.
1 Answer 1
val calculateHeight = calculateHeight()
This line is calling the calculateHeight() function. That function reads the contents of your fields at that moment in time and performs calculations upon their contents. The result of the calculateHeight() function is then stored in the variable, itself confusingly named calculateHeight .
You then do not do anything meaningful with calculateHeight . You reference that variable twice, in statements that then do not do anything:
Those lines will give you «expression is unused», because the expression ( calculateHeight ) is unused. You are not doing anything with it.
it still always comes to 30.48cm
You do not state where and how you are seeing any results. If I had to guess, displayText() is supposed to something like that, given the name of that function. Your question does not include the source for displayText() , so we are having to guess.
But, it is unclear where displayText() is getting anything to display. You are not passing any parameters to displayText() . Perhaps you should have displayText(calculateHeight) , and have your displayText() function take that parameter and do something with it. Or, perhaps displayText() should be calling calculateHeight() directly, and you can remove all the calculateHeight() / calculateHeight stuff from your calculateButton() function.
«the expression is unused» when using kotest in intellij
With this kotlin test written with Kotest, IntelliJ shows the warning «The expression is unused» and syntax coloration is not working. Also, when running the tests, the test is not found.
class TalentMatchServiceSpec : StringSpec() < init < "add 3 to 2 should give 5" < // Given integers 2 and 3 val two = 2 val three = 3 // When adding both numbers val sum = two + three // Then the result is 5 sum shouldBe 5 >> >
2 Answers 2
@Thomas Martin’s answer is correct, but you asked why it makes a difference.
The SpringSpec in Kotest relies on some Kotlin DSL features to work. Let’s demonstrate by building the same function from scratch.
We would start by using Kotlin’s extension functions. This allows us to add functions to classes we don’t control.
fun String.test(test: () -> Unit)
So then we can call this test function on any string:
Secondly, Kotlin allows any final lambda arg to be brought outside of the parens. So foo(arg, arg2, arg3, lambda) can become foo(arg, arg2, arg3) lambda . This means we can write our test as:
Next, we can mark functions as infix and then we do not need to use the dot to invoke them. So our test function becomes:
infix fun String.test(test: () -> Unit)
And our test now looks like:
Finally, any function named invoke and marked as an operator function can be invoked without the function name. So operator fun invoke(a: String, b: String) inside a class Foo can be invoked as both the regular Foo.invoke(a,b) or just Foo(a, b) .
So putting all this together, our final test function looks like:
operator fun String.invoke(test: () -> Unit)
If the braces are moved to the next line, as in your original question, it just looks like a String followed by an unrelated lambda block. Two different expressions.
Jetpack Compose Kotlin says
I’m passing a lambda to a composable to trigger on Modifier.clickable but the compiler warns that «The expression is unused» Why would this be happening and how to correct it? Very weird? Screenshot of compiler warning
@Composable fun MainScreen(dateVM: DateViewModel = DateViewModel(), dateList: List) < val dayIndexState = remember < mutableStateOf(0) >Column < ShowDateScroll(dateIndex = dayIndexState.value) < newDateIndex ->dayIndexState.value = newDateIndex > > > @Composable fun ShowDateScroll( dateIndex: Int, dateIndexUpdate: (newIndexState: Int) -> Unit ) < LazyRow( modifier = Modifier .fillMaxWidth() .background(Color.LightGray), horizontalArrangement = Arrangement.spacedBy(25.dp) ) < items(items = dateList, itemContent = < dateItem ->Column( horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier .clickable( role = Role.Button ) < dateIndexUpdate >) < Text( text = dateItem.dow ) Text( text = dateItem.day.toString(), ) >>) > >
2 Answers 2
the code you passed in the trailing lambda will never execute.
A trailing lambda itself is a function, if you just write down another function name nothing will happen. Compare to:
If you never use the function / invoke it. Thus it is unused.
In order to invoke the code you have different options: Replace the trailing lambda with the function reference
Modifier.clickable( role = Role.Button, onClick = dateIndexUpdate, )
Another way is to invoke the function:
Modifier.clickable( role = Role.Button, ) < dateIndexUpdate() /* or */ dateIndexUpdate.invoke() >
In your code the second option can be used with clickable because you need to provide the parameter:
Modifier.clickable( role = Role.Button, ) < dateIndexUpdate(dateIndex) /* or */ dateIndexUpdate.invoke(dateIndex) >
Not sure if it is the correct index, you need to adjust it for your code.
[Solved]-How do I get rid of this ‘warning: the expression is unused’ warning?-kotlin
calling foo() explicitely on this will make the warning go away:
operator fun invoke(foo: test.() -> boolean): boolean
operator fun invoke(foo: test.() -> boolean): boolean
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
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.
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.
Related Query
- How do I get rid of this ‘warning: the expression is unused’ warning?
- How to get rid of Incremental annotation processing requested warning during the build?
- How to get rid of from SpreadOperator performance warning that was given by Detekt while using Spring Boot?
- There are multiple good constructors and Room will pick the no-arg constructor. How to solve this warning
- How to get rid of this boilerplate code in this sealed class hierarchy?
- How to get the compiler to say what type it thinks an expression is?
- How do i fix this Kodein error: Expression ‘kodein’ cannot be invoked as a function. The function ‘invoke()’ is not found
- How to get rid of the value using a Gson TypeAdapter?
- How to get variable’s value in a lambda expression from outside the function in Kotlin
- How can I get the Stacktrace of this error of fix it?
- How to only get rid of the refresh widget once reload is fully done
- How does this android code get the activity from ? material-components-android-codelabs
- kotlin — can I rewrite this map access to get rid of the «!!«?
- How to get rid of this lines when copying?
- How to write this regular expression correctly in Kotlin to test the code in Pascal
- What does Any mean in this context? Why does adding `:Any` get rid of the compiler error?
- How to change this function to get rid of Redundant SAM-constructior?
- How to get rid of the external white background of RoundedCornerShape
- Kotlin — how to get rid of this compiler error?
- How to get the current index in for each Kotlin
- How do I get the current time as a TimeStamp in Kotlin?
- How can I get the time it takes a function to test the performance of functions in Kotlin
- How can I obfuscate my sdk coded with kotlin (and get rid of Metadata)
- How does kotlin use this by delegate to instantiate the viewmodel
- The lambda expression is unused
- How to get the name of a coroutine in Kotlin?
- How is putting the lambda expression after the parameters on a mapTo call legal syntax?
- How to get the current working directory in Kotlin?
- How do I get the document ID for a Firestore document using kotlin data classes
- How to negate a boolean expression when using the elvis operator in kotlin?
More Query from same tag
- Sorting Secondary Collection using Primary Sorted Collection’s field
- Developing lambda expression
- How to optimize code to not using `.compute` and without using `!!` in Kotlin
- restAssured — cannot master post method
- Comparing string object dates in Android Kotlin?
- Custom HashMap class get argument type mismatch when using with @RequestParam in Kotlin
- Converting a generic enum from Java to Kotlin
- Create Vertical Divider Jetpack Compose
- Fragment is overlapping on activity :Android Studio
- Class cast exception between parent and child classes
- Convert android Android Icon to Bitmap in Android
- How to use Mockito.mockStatic for mocking static methods in kotlin android
- Gradle failure — null VariantManager
- Getting RuntimeException for kotlin code
- Kotlin — «Don’t care» in desugaring of data class
- How to run Dokka offline in Android
- Unable to create @Body converter for class in Retrofit using Moshi
- Is there any method in kotlin to get digit at specific index in an «Int»?
- Can’t use HiltAndroidRule in testing Room Dao
- DiffCallBack Isn’t Getting Called?
- React Native — Make sure Gradle is running on a JDK, not JRE
- Any data type not working in data class kotlin
- Querying firebase database
- Show html string with inline style attribute in android text view
- Minimum of 2 nullable numbers
- kotlin passing a mutableList in the onNext or a BehaviorSubject which should not be null
- How to import Bootstrap library in Kotlin-React-App
- DBFlow SQLite.delete() throwing java.lang.IllegalArgumentException: Please use query()
- How can I access the nested value in a JSON payload using Gson library with Kotlin
- How can I extract date and time from a Long type variable in Kotlin on Android