explicit generics annotation for constructor#734
Conversation
ddworak
left a comment
There was a problem hiding this comment.
.apply case class instantiation should be handled as well for symmetry, this is even more common
true, I've even created a test for it xd. Will be done |
# Conflicts: # analyzer/src/main/scala/com/avsystem/commons/analyzer/ExplicitGenerics.scala
There was a problem hiding this comment.
Pull request overview
This PR extends the @explicitGenerics analyzer rule to support constructors in addition to methods. Previously, the annotation only enforced explicit type parameters on annotated methods. Now it also works on class and case class constructors, requiring explicit type arguments when instantiating annotated generic classes.
Changes:
- Extended
ExplicitGenericsanalyzer to detect and report on constructors (viaNewnodes) with inferred type parameters - Added logic to detect case class
applymethods when the case class itself is annotated, ensuring consistency - Added test fixtures and comprehensive test cases for the new constructor support
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| analyzer/src/main/scala/com/avsystem/commons/analyzer/ExplicitGenerics.scala | Core implementation: added New case pattern matching, applyOfAnnotatedCompanion helper, and refactored error reporting |
| analyzer/src/test/scala/com/avsystem/commons/analyzer/TestUtils.scala | Added test fixtures: GenericClass[T] and GenericCaseClass[T] annotated with @explicitGenerics |
| analyzer/src/test/scala/com/avsystem/commons/analyzer/ExplicitGenericsTest.scala | Added four new test cases covering constructor and apply method scenarios with both inferred and explicit type arguments |
| analyzer/src/test/scala/com/avsystem/commons/analyzer/ImplicitValueClassesTest.scala | Minor formatting cleanup: removed trailing commas and adjusted formatting style for consistency |
Comments suppressed due to low confidence (3)
analyzer/src/main/scala/com/avsystem/commons/analyzer/ExplicitGenerics.scala:41
- The companion class resolution logic here doesn't match the logic in
applyOfAnnotatedCompanion. This line directly accessespre.symbol.owner.companionClass, but the owner could be either a Module or ModuleClass. InapplyOfAnnotatedCompanion, the code handles both cases by checkingisModuleClassandisModule. This discrepancy could lead to getting NoSymbol or the wrong symbol in some cases. Consider extracting the companion class resolution into a helper function or reusing thecompanionClsvalue fromapplyOfAnnotatedCompanion.
val targetSym = if (applyOfAnnotatedCompanion(pre.symbol)) pre.symbol.owner.companionClass else pre.symbol
analyzer/src/test/scala/com/avsystem/commons/analyzer/ExplicitGenericsTest.scala:74
- The test case is missing a test for explicit type arguments on case class constructors using the
newkeyword (e.g.,new TestUtils.GenericCaseClass[Int](123)). While the test at line 68-74 covers explicit type args for regular classes, it would be good to also verify that case classes work correctly with explicit type parameters when usingnew.
test("explicit in constructor should not be rejected") {
assertNoErrors(scala"""
|import com.avsystem.commons.analyzer.TestUtils
|
|val x = new TestUtils.GenericClass[Int]()
|""".stripMargin)
}
analyzer/src/test/scala/com/avsystem/commons/analyzer/ExplicitGenericsTest.scala:66
- Missing test case for case class apply method with explicit type arguments (e.g.,
TestUtils.GenericCaseClass[Int](123)). The test suite covers the error case (inferred type args in apply) but doesn't verify that the non-error case (explicit type args in apply) works correctly.
test("inferred in apply when constructor marked should be rejected") {
assertErrors(
1,
scala"""
|import com.avsystem.commons.analyzer.TestUtils
|
|val x = TestUtils.GenericCaseClass(123)
|""".stripMargin,
)
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
I've found that someone had used it for a constructor but it's no supported xd