mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 06:47:34 +00:00
Kernel: Stop allowing implicit conversion from KResult to int
This patch removes KResult::operator int() and deals with the fallout. This forces a lot of code to be more explicit in its handling of errors, greatly improving readability.
This commit is contained in:
parent
d30d776ca4
commit
7676edfb9b
14 changed files with 51 additions and 52 deletions
|
@ -28,12 +28,17 @@ public:
|
|||
: m_error(0)
|
||||
{
|
||||
}
|
||||
operator int() const { return m_error; }
|
||||
[[nodiscard]] int error() const { return m_error; }
|
||||
|
||||
[[nodiscard]] bool is_success() const { return m_error == 0; }
|
||||
[[nodiscard]] bool is_error() const { return !is_success(); }
|
||||
|
||||
bool operator==(ErrnoCode error) const { return is_error() && m_error == -error; }
|
||||
bool operator!=(ErrnoCode error) const { return !is_error() || m_error != -error; }
|
||||
|
||||
bool operator!=(KSuccessTag) const { return is_error(); }
|
||||
bool operator==(KSuccessTag) const { return !is_error(); }
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
friend class KResultOr;
|
||||
|
@ -167,9 +172,11 @@ private:
|
|||
}
|
||||
|
||||
template<>
|
||||
struct AK::Formatter<Kernel::KResult> : Formatter<int> {
|
||||
struct AK::Formatter<Kernel::KResult> : Formatter<FormatString> {
|
||||
void format(FormatBuilder& builder, Kernel::KResult value)
|
||||
{
|
||||
return Formatter<int>::format(builder, value);
|
||||
if (value.is_error())
|
||||
return AK::Formatter<FormatString>::format(builder, "KResult({})", value.error());
|
||||
return AK::Formatter<FormatString>::format(builder, "KResult(success)");
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue