Skip to content

Commit 722c50f

Browse files
authored
Code review agent: unnecessary generic type param (#16949)
1 parent e09d379 commit 722c50f

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

.github/agents/knowledge/general-rules.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ When a "Knowledge File" is listed, load it from `knowledge/` before reviewing th
3030
| Build | Gradle conventions, muzzle, test tasks, plugins | `build.gradle.kts`, `settings.gradle.kts` | `gradle-conventions.md` |
3131
| Build | `testcontainersBuildService` declaration | Testcontainers dependency without `usesService` | `gradle-conventions.md` |
3232
| Style | Prefer instance creation over singletons for stateless interface impls (except on hot paths or Kotlin `object` declarations) | `TextMapGetter`, `TextMapSetter`, `*AttributesGetter`, `AttributesExtractor`, `SpanNameExtractor`, `HttpServerResponseMutator`, enum/static singletons ||
33+
| Style | No unnecessary explicit type witnesses on generic method calls (`Collections.<String>emptyList()`) | Java generic method calls with explicit type parameters ||
3334
| Style | Remove redundant null guards on attribute puts | `AttributesBuilder.put`, `onStart`, `onEnd`, attribute extraction methods ||
3435
| General | No redundant `ByteBuffer.duplicate()` on `Value.getValue()` | `Value.getValue()` with `BYTES` type, `ByteBuffer` handling ||
3536
| Style | Nullability correctness — no guards for non-nullable params; add `@Nullable` when null is actually passed/returned/stored; respect upstream SDK `@Nullable` contracts for `TextMapGetter`/`TextMapSetter` | `TextMapGetter`, `TextMapSetter`, `*AttributesGetter`, `*Extractor` implementations, null checks, missing `@Nullable`, fields assigned from `@Nullable` sources ||
@@ -255,6 +256,32 @@ Preferred:
255256
ByteBuffer byteBuffer = (ByteBuffer) value.getValue();
256257
```
257258

259+
## [Style] No Unnecessary Explicit Type Parameters on Method Calls
260+
261+
Since Java 5, the compiler infers generic type arguments in virtually all contexts.
262+
Explicit type witnesses on method calls (e.g., `Collections.<String>emptyList()`) are
263+
almost never needed and add visual noise.
264+
265+
Flag any explicit type witness that is not required for compilation:
266+
267+
```java
268+
// BAD — type witness is unnecessary
269+
return Collections.<String>emptyList().iterator();
270+
Iterator<String> it = Collections.<String>emptyList().iterator();
271+
```
272+
273+
Preferred:
274+
275+
```java
276+
// GOOD — compiler infers the type argument
277+
return Collections.emptyList().iterator();
278+
Iterator<String> it = Collections.emptyList().iterator();
279+
```
280+
281+
**Exception**: an explicit type witness is acceptable only when the compiler cannot infer
282+
the type without it (e.g., the call is used as a method argument where the target type is
283+
ambiguous and the cast would otherwise be required). Do not flag those cases.
284+
258285
## [Semconv] Constants by Module Type
259286

260287
- `library/src/main/`: incubating semconv constants (from

0 commit comments

Comments
 (0)