- 
  Notifications
 You must be signed in to change notification settings 
- Fork 1.1k
 
 Add lint check for wildcards in inline match
 
 #23642
 
 -
Combining wildcards case _ and inline match can lead to unexpected behaviours when providing runtime computed values.
Here's an example:
inline def stringValue(value: Int): String = { inline value match case 1 => "t1" case 2 => "t2" case _ => "tx" } @main def Test = val arg = Option(1).get println(arg) // 1 println(stringValue(1)) // "t1" println(stringValue(arg)) // "tx"
In this example if we ch about stringValue being computed at compile time we can easily introduce logic bugs to our code base.
To prevent that we need to either:
- require that valueis a cosnt usingscala.ompiletime.requireConst
- not use wildcard match
Both of these have it's own drawbacks, and each of these rules is easy to forget about. I propose we try to introduce a lint check under a flag (e.g. -Winline-match-wildcard) to detect and warn about such usages.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments
-
I wonder if this should actually be the default. Can we ever inline properly with a wildcard present?
Beta Was this translation helpful? Give feedback.
All reactions
-
This is standard behavior of an inline match. If that's not the behavior you want, use a regular match. I don't see how this is different from any other situation where two cases are not disjoint. If want to lint this away, you should lint every non-disjoint pair of cases. But if you do that, inline match does not have any useful behavior that match doesn't have anymore.
Beta Was this translation helpful? Give feedback.