Make getter return value optional#12
Conversation
📝 WalkthroughWalkthroughGetter functions were changed to return Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
src/SlvCtrlProtocol.h (2)
180-182:⚠️ Potential issue | 🔴 CriticalBug:
std::optional<bool>in boolean context checkshas_value(), not the contained value.
this->getValue()now returnsstd::optional<bool>. The ternarythis->getValue() ? "true" : "false"invokesstd::optional::operator bool(), which tests whether a value is present, not what the value is. Consequences:
Getter returns has_value()Output Correct? optional(true)true"true"✅ coincidence optional(false)true"true"❌ should be "false"nulloptfalse"false"❌ misleading This silently inverts
falsevalues to"true".🐛 Proposed fix
void writeValue(ISlvCtrlOut& out) const override { - out.write(this->getValue() ? "true" : "false"); + auto val = this->getValue(); + if (val.has_value()) { + out.write(val.value() ? "true" : "false"); + } }
333-347:⚠️ Potential issue | 🟡 Minor
StrAttributenow relies on basewriteValue— beware ofnullptrinside the optional.With the removed
writeValueoverride,BaseAttribute<const char*>::writeValuewill callout.write(val.value()). If a getter returnsstd::optional<const char*>(nullptr)(i.e., has a value, but that value isnullptr), this passesnullptrtoISlvCtrlOut::write(const char*), which may cause undefined behavior depending on the implementation.
🤖 Fix all issues with AI agents
In `@src/SlvCtrlProtocol.h`:
- Around line 110-115: The current writeValue implementation silently omits
output when getValue() is nullopt which causes ambiguous empty values; change
the contract so absence is propagated: update writeValue(ISlvCtrlOut& out) to
return a bool (or optional) indicating whether a value was written (use
getValue() and if has_value() call out.write(...) and return true, else return
false), then update the caller cmdGet to check that return value and emit an
explicit sentinel (e.g., "null") or a non-ok status when writeValue returned
false; reference writeValue, getValue, cmdGet and ISlvCtrlOut::write when making
these changes.
Summary by CodeRabbit