@@ -136,7 +136,7 @@ class IsInstanceOfEvaluator extends MiniPhaseTransform { thisTransformer =>
136
136
137
137
// Check if the selector's potential type parameters will be erased, and if so warn
138
138
val selTypeParam = tree.args.head.tpe.widen match {
139
- case tp @ AppliedType (_, arg :: _) =>
139
+ case tp @ AppliedType (_, args @ ( arg :: _) ) =>
140
140
// If the type is `Array[X]` where `X` is a primitive value
141
141
// class. In the future, when we have a solid implementation of
142
142
// Arrays of value classes, we might be able to relax this check.
@@ -146,6 +146,16 @@ class IsInstanceOfEvaluator extends MiniPhaseTransform { thisTransformer =>
146
146
// has @unchecked annotation to suppress warnings
147
147
val hasUncheckedAnnot = arg.hasAnnotation(defn.UncheckedAnnot )
148
148
149
+ // Shouldn't warn when matching on a subclass with underscore
150
+ // params or type binding
151
+ val matchingUnderscoresOrTypeBindings = args.forall(_ match {
152
+ case tr : TypeRef =>
153
+ tr.symbol.is(BindDefinedType )
154
+ case TypeBounds (lo, hi) =>
155
+ (lo eq defn.NothingType ) && (hi eq defn.AnyType )
156
+ case _ => false
157
+ }) && selector <:< scrutinee
158
+
149
159
// we don't want to warn when matching on `List` from `Seq` e.g:
150
160
// (xs: Seq[Int]) match { case xs: List[Int] => ??? }
151
161
val matchingSeqToList = {
@@ -160,13 +170,16 @@ class IsInstanceOfEvaluator extends MiniPhaseTransform { thisTransformer =>
160
170
hasSameTypeArgs
161
171
}
162
172
163
- if (! topType && ! hasUncheckedAnnot && ! matchingSeqToList && ! anyValArray) {
164
- ctx.uncheckedWarning(
165
- ErasedType (hl """ |Since type parameters are erased, you should not match on them in
166
- | ${" match" } expressions. """ ),
167
- tree.pos
168
- )
169
- }
173
+ val shouldWarn =
174
+ ! topType && ! hasUncheckedAnnot &&
175
+ ! matchingUnderscoresOrTypeBindings && ! matchingSeqToList &&
176
+ ! anyValArray
177
+
178
+ if (shouldWarn) ctx.uncheckedWarning(
179
+ ErasedType (hl """ |Since type parameters are erased, you should not match on them in
180
+ | ${" match" } expressions. """ ),
181
+ tree.pos
182
+ )
170
183
true
171
184
case _ =>
172
185
if (tree.args.head.symbol.is(TypeParam )) {
0 commit comments