Improve build pipelines and code quality - #132
Conversation
leonklingele
left a comment
There was a problem hiding this comment.
Thanks for the PR, @sansmoraxz! 😊 Always very happy to see your contributions to the repo!
Left some smaller nits but also changes which, imo, are blockers. Definitely open to discuss things though! 😃
|
|
||
| adu[length-1] = byte(checksum >> 8) | ||
| adu[length-2] = byte(checksum) | ||
| adu[length-2] = byte(checksum & 0x00ff) |
There was a problem hiding this comment.
To other reviewers wondering why this change was made:
Why: To make the developer's intent explicitly clear and visually symmetrical.
In Modbus RTU, the 16-bit CRC (Cyclic Redundancy Check) is appended to the end of the packet, sending the lower byte first, followed by the upper byte.
In Go, converting a 16-bit integer to a byte using byte(checksum) automatically drops the higher 8 bits and keeps the lower 8 bits. It works perfectly fine on its own.
However, by adding the bitwise AND mask (& 0x00ff), the code explicitly states: "I am intentionally extracting the lower 8 bits here."
It also provides nice visual symmetry with the line immediately above it (byte(checksum >> 8)), showing exactly how the 16 bits are being split into two 8-bit chunks. Additionally, explicit masking like this often pacifies strict static analysis tools (linters) that might warn about implicit data truncation.
Thanks @sansmoraxz! 😊
There was a problem hiding this comment.
TODO @leonklingele: Lots of changes, still open for review.
There was a problem hiding this comment.
All read funcs (and some of the write funcs) still use common / duplicate code. For consistency, I'd prefer to either duplicate code everywhere (as before), or unify it for all read + write funcs.
Co-authored-by: leonklingele <git@leonklingele.de>
Co-authored-by: leonklingele <git@leonklingele.de>
Co-authored-by: leonklingele <git@leonklingele.de>
|
I was thinking, perhaps it would be better to have typed errors instead of string builders scattered across codebase. WDYT? Anyway might be better to keep it out of this PR, as that's a large behaviour change. |
Fixes: #131
Added
golangci-lintwith a couple of rules that made sense.And their related fixes.
revive: the defacto golint replacementgovet: just simplifiedgo vetgosec: Scan for potential security issues. Did catch a couple of casting related bugs in code.gocritic: highly configurable. did catch the exit and defer skip in cli for err case and some comment spacing.staticcheck: https://staticcheck.dev/errcheck: catch where you forgot to handleerrreturneddupl: Code deduplication scanner.You can check other rules here: https://golangci-lint.run/docs/linters/configuration.
Updated makefile to use
golangci-lintinstead ofgolint.Also CI pipeline has really old versions of golangci-lint.
The provided
.golangci.ymlwould not be accepted by that version. Ensure to update before merging.Maintainers already granted write access to this branch.