Skip to content

Sonar Category C cleanup: dead code + redundant casts (S1905, S1068, S1144, S1130) - #3395

Merged
dbwiddis merged 4 commits into
oshi:masterfrom
dbwiddis:sonar-cleanup-mechanical
Jun 14, 2026
Merged

dbwiddis merged 4 commits into
oshi:masterfrom
dbwiddis:sonar-cleanup-mechanical

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 14, 2026 •

Copy link
Copy Markdown
Member

Mechanical dead-code / redundancy cleanup from the Sonar triage backlog, one commit per rule. No behavior change; verified locally with a clean install -P aggregate-coverage,native-comparison (full unit suite + NativeComparisonTest green) plus checkstyle/forbiddenapis/javadoc on the touched modules.

Fixed

  • S1905 (18) — removed redundant casts. Each verified safe: every (long) i * X had a long right operand (so the product was already computed in long — no overflow was being prevented), the (int) casts on short/byte are redundant under & promotion, and getPartitions() is already List<HWPartition>.
  • S1068 (8) — deleted genuinely dead private fields (unused loggers, write-only fields whose derived state is what's used, leftover constants).
  • S1144 (4) — removed dead constructor/method overloads (pre-diskType constructors; an inlined queryLspciMemorySize(String)). These are not prevent-instantiation idioms.
  • S1130 (1) — removed a vestigial throws Exception on MacInstalledApps.readStringValue.

Notable: a bug not introduced

DiskMetrics.diskStores looks like an unused field (S1068) but is a deliberate GC root — Micrometer's FunctionCounter only holds a WeakReference to each disk object, so this strong reference keeps them alive. Deleting it would silently break the disk metrics after a GC. Kept it, documented, and marked @SuppressWarnings("java:S1068").

Intentionally deferred

  • S1130 ×6 (JMXOshiAgentServer) — @Override stubs whose throws mirror the JMX MBeanServer interface contract; removing Sonar's flagged subset would make them inconsistent with their siblings. Better suppressed-as-conformance or left.

Remaining Category C (S1168 null→empty, S4144 identical bodies) involves caller-by-caller analysis and is a deliberate follow-up.

Summary by CodeRabbit

  • Refactor

    • Simplified native memory operations and pointer handling across platform implementations.
    • Removed unnecessary type conversions throughout codebase.
    • Consolidated constructor signatures for improved consistency.
    • Eliminated unused constants and helper methods.
  • Chores

    • Removed unused logging dependencies from several components.
    • Added documentation to prevent unintended garbage collection of internal references.
  • Tests

    • Improved test setup infrastructure for metrics validation.

dbwiddis added 4 commits June 13, 2026 20:17
Drop 18 redundant casts. Each was verified safe: every '(long) i * X'
multiplication has a long right operand (X is increment, PTR_SIZE,
ENUM_SERVICE_STATUS_PROCESS_SIZE, ADDRESS.byteSize(), etc.), so the product was
already computed in long and no narrowing/overflow was being prevented. The two
'(int)' casts on short/byte values are redundant because the '&' operator
already promotes to int, and getPartitions() is already declared List<HWPartition>.
No behavior change.
Delete eight genuinely dead private fields: unused loggers (MacGpuStatsFFM,
WindowsFileSystemFFM, DragonFlyBsdOSProcessJNA), write-only fields whose derived
state is what's actually used (MacGpuStatsFFM.cardName, CupsFunctions.CUPS_LIBRARY
- the bound method handles use the local lookup), and leftover constants
(AdlUtilFFM.PERF_GPU_ACTIVITY_OFFSET, WindowsGraphicsCardJNA.MB_TO_BYTES from a
removed LHM code path, OshiMetricsTest.hal).

NOT deleted: DiskMetrics.diskStores. It looks unused but is a deliberate strong
reference (GC root) that keeps the disk objects alive, because Micrometer's
FunctionCounter only holds WeakReferences to them; deleting it would silently
break the disk metrics after a GC. Documented and marked @SuppressWarnings("java:S1068").
Delete four dead overloads (not prevent-instantiation idioms - each class is
instantiated via its other constructor): the pre-diskType constructors of
MacHWDiskStoreFFM / WindowsHWDiskStoreFFM / WindowsHWDiskStoreJNA (the diskType
variants are the ones getDisks() uses), and LinuxGraphicsCard's
queryLspciMemorySize(String) overload, which nothing calls - the caller already
runs lspci and invokes the List<String> overload directly. No behavior change.
MacInstalledApps.readStringValue declared 'throws Exception' but does only string
parsing and cannot throw it. Removing the clause is safe: the sole caller wraps
it in a catch(Exception) that remains valid (it still catches RuntimeException
and other calls in the same try). No behavior change.

The six JMXOshiAgentServer S1130 findings are intentionally left: they are
@OverRide stubs whose throws clauses mirror the JMX MBeanServer interface
contract (Java forbids an override from adding new checked exceptions), so
removing Sonar's flagged subset would make them inconsistent with their siblings.
@coderabbitai

coderabbitai Bot commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 06424a1e-fadd-43d9-84ef-0654c0575727

📥 Commits

Reviewing files that changed from the base of the PR and between 89090ff and 6a351d2.

📒 Files selected for processing (24)
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxHWDiskStore.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacInstalledApps.java
  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/aix/PsInfoFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/ForeignFunctions.java
  • oshi-core-ffm/src/main/java/oshi/ffm/platform/unix/CupsFunctions.java
  • oshi-core-ffm/src/main/java/oshi/ffm/platform/windows/Shell32FFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/platform/windows/com/ComObjectFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WbemcliUtilFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacGpuStatsFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsHWDiskStoreFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsFileSystemFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOperatingSystemFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsGraphicsCardJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsHWDiskStoreJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessJNA.java
  • oshi-core/src/main/java/oshi/util/gpu/AdlUtilJNA.java
  • oshi-metrics/src/main/java/oshi/metrics/DiskMetrics.java
  • oshi-metrics/src/test/java/oshi/metrics/OshiMetricsTest.java
💤 Files with no reviewable changes (10)
  • oshi-core-ffm/src/main/java/oshi/ffm/platform/unix/CupsFunctions.java
  • oshi-metrics/src/test/java/oshi/metrics/OshiMetricsTest.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsHWDiskStoreJNA.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGraphicsCard.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsHWDiskStoreFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsGraphicsCardJNA.java
  • oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java
  • oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessJNA.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacGpuStatsFFM.java
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-04-12T19:22:09.716Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:22:09.716Z
Learning: When reviewing OSHI hardware platform code that uses or implements `CentralProcessor`/`PhysicalProcessor.getEfficiency()`, remember the API’s counterintuitive semantics: a *higher* efficiency-class value indicates *greater performance* (lower efficiency). P-cores/“performance cores” should therefore have higher values (e.g., 1) than E-cores/“efficiency cores” (e.g., 0), as documented in the `CentralProcessor` Javadoc. Do not flag code as incorrect solely because ARM_P_CORES or Intel P-cores are assigned higher efficiency values.

Applied to files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxHWDiskStore.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacInstalledApps.java
🔇 Additional comments (14)
oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsFileSystemFFM.java (1)

187-187: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxHWDiskStore.java (1)

129-129: LGTM!

oshi-common/src/main/java/oshi/software/common/os/mac/MacInstalledApps.java (1)

290-290: LGTM!

oshi-metrics/src/main/java/oshi/metrics/DiskMetrics.java (1)

49-53: LGTM!

oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java (1)

112-112: LGTM!

Also applies to: 130-130

oshi-core-ffm/src/main/java/oshi/driver/unix/aix/PsInfoFFM.java (1)

92-92: LGTM!

oshi-core-ffm/src/main/java/oshi/ffm/ForeignFunctions.java (1)

134-134: LGTM!

Also applies to: 137-137

oshi-core-ffm/src/main/java/oshi/ffm/platform/windows/Shell32FFM.java (1)

64-64: LGTM!

Also applies to: 67-67

oshi-core-ffm/src/main/java/oshi/ffm/platform/windows/com/ComObjectFFM.java (1)

59-59: LGTM!

oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WbemcliUtilFFM.java (1)

254-254: LGTM!

Also applies to: 260-260

oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.java (1)

66-66: LGTM!

Also applies to: 89-89

oshi-core-ffm/src/main/java/oshi/software/os/unix/openbsd/OpenBsdOSProcessFFM.java (1)

72-72: LGTM!

Also applies to: 106-106

oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOperatingSystemFFM.java (1)

183-183: LGTM!

oshi-core/src/main/java/oshi/util/gpu/AdlUtilJNA.java (1)

75-75: LGTM!


📝 Walkthrough

Walkthrough

Across 23 files in oshi-common, oshi-core, oshi-core-ffm, and oshi-metrics, this PR removes unused SLF4J logger fields, dead private constants and methods, redundant private constructor overloads (consolidating to diskType-aware constructors), and unnecessary explicit numeric/type casts. A suppression annotation and comment are added to DiskMetrics.diskStores, and test/API signatures receive minor cleanup.

Changes

Code Cleanup: Dead Code, Cast, and Logger Removal

Layer / File(s) Summary
Unused fields, constants, and private method removal
oshi-common/.../LinuxGraphicsCard.java, oshi-core-ffm/.../CupsFunctions.java, oshi-core-ffm/.../AdlUtilFFM.java, oshi-core/.../WindowsGraphicsCardJNA.java, oshi-core-ffm/.../MacGpuStatsFFM.java
Removes the queryLspciMemorySize(String) private overload, the cached CUPS_LIBRARY field, the PERF_GPU_ACTIVITY_OFFSET constant, the MB_TO_BYTES constant, and the raw cardName instance field.
Unused SLF4J logger removal
oshi-core-ffm/.../MacGpuStatsFFM.java, oshi-core-ffm/.../WindowsFileSystemFFM.java, oshi-core/.../DragonFlyBsdOSProcessJNA.java
Deletes Logger/LoggerFactory imports and LOG field declarations where the logger was declared but never used at call sites.
Disk store constructor consolidation
oshi-core-ffm/.../MacHWDiskStoreFFM.java, oshi-core-ffm/.../WindowsHWDiskStoreFFM.java, oshi-core/.../WindowsHWDiskStoreJNA.java
Removes the 4-parameter private constructors (without diskType), retaining only the 5-parameter overloads that forward diskType to the superclass.
Unnecessary explicit numeric/type cast removal
oshi-core-ffm/.../WhoFFM.java, oshi-core-ffm/.../PsInfoFFM.java, oshi-core-ffm/.../ForeignFunctions.java, oshi-core-ffm/.../Shell32FFM.java, oshi-core-ffm/.../ComObjectFFM.java, oshi-core-ffm/.../WbemcliUtilFFM.java, oshi-core-ffm/.../FreeBsdOSProcessFFM.java, oshi-core-ffm/.../OpenBsdOSProcessFFM.java, oshi-core-ffm/.../WindowsFileSystemFFM.java, oshi-core-ffm/.../WindowsOperatingSystemFFM.java, oshi-core/.../AdlUtilJNA.java
Removes redundant (long), (int), and (ValueLayout) casts from FFM offset arithmetic and allocations, relying on implicit numeric widening and return-type inference.
Minor API, annotation, and test cleanup
oshi-common/.../LinuxHWDiskStore.java, oshi-common/.../MacInstalledApps.java, oshi-metrics/.../DiskMetrics.java, oshi-metrics/src/test/.../OshiMetricsTest.java
Removes an unchecked cast in getMutablePartitionList(), drops throws Exception from readStringValue, adds @SuppressWarnings("java:S1068") with a comment to DiskMetrics.diskStores, and refactors the test to drop unused HAL field while adding OperatingSystem os.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • oshi/oshi#3202: This PR removes LinuxGraphicsCard.queryLspciMemorySize(String), directly continuing the lspci parsing refactor started there.
  • oshi/oshi#3393: Overlaps at the code level with the WbemcliUtilFFM.populateRow VARIANT handling changes, where both PRs adjust unsigned type cast behavior.

Poem

🐇 Hopping through the code today,
I swept old casts and fields away—
No logger left to gather dust,
No unchecked cast, no (long) a must.
The warren's tidy, clean, and bright—
Less noise means every hop feels right! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: mechanical code cleanup addressing four Sonar rules (S1905, S1068, S1144, S1130) to remove dead code and redundant casts.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Jun 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.08%. Comparing base (89090ff) to head (6a351d2).

Files with missing lines Patch % Lines
...oshi/ffm/util/platform/windows/WbemcliUtilFFM.java 0.00% 2 Missing ⚠️
...oshi/software/os/windows/WindowsFileSystemFFM.java 0.00% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (66.66%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3395      +/-   ##
============================================
+ Coverage     75.02%   75.08%   +0.05%     
  Complexity       76       76              
============================================
  Files           579      579              
  Lines         21364    21355       -9     
  Branches       3556     3556              
============================================
+ Hits          16029    16035       +6     
+ Misses         3311     3300      -11     
+ Partials       2024     2020       -4     
Flag Coverage Δ
freebsd 26.32% <22.22%> (+0.01%) ⬆️
linux 30.93% <11.11%> (+0.02%) ⬆️
macos 31.24% <0.00%> (+0.01%) ⬆️
openbsd 25.81% <22.22%> (+0.02%) ⬆️
solaris 27.87% <0.00%> (+<0.01%) ⬆️
windows 39.55% <11.11%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit cbfdddd into oshi:master Jun 14, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the sonar-cleanup-mechanical branch June 14, 2026 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant