You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This rule would forbid comparing the result of @intFromPtr(...) with zero, unless the pointer is allowzero (allowzero pointers are rare enough that, if type analysis isn't able to determine pointer annotations, it might be acceptable if you need to manually disable this rule for @intFromPtr calls on allowzero pointers).
For pointers that are neither optional nor allowzero, @intFromPtr will never return zero because such pointers are forbidden from being zero. Optimization knows this and makes a comparison with zero always return false. So this comparison is always a bug.
For optional, non-allowzero pointers, @intFromPtr(p) != 0 does have the intended behavior, but I think it's cleaner to write p != null which is why I'm proposing this rule apply to those pointers too.
pubconstHeader=struct {
name: []constu8,
pubfnisMultiline(self: Header) bool {
returnself.name.len==0;
}
};
pubconstExample=struct {
optional_data: ?*u32,
pubfnhasData(self: Example) bool {
returnself.optional_data!=null;
}
};
// for this example suppose you're writing an OS or somethingpubconstMemoryMapping=struct {
pages: []allowzeroalign(4096) constu8,
pubfnisStartOfAddressSpace(self: MemoryMapping) bool {
// this code is correct and there's not a better way to express itreturn@intFromPtr(self.pages.ptr) ==0;
}
};
The text was updated successfully, but these errors were encountered:
Description
This rule would forbid comparing the result of
@intFromPtr(...)
with zero, unless the pointer isallowzero
(allowzero
pointers are rare enough that, if type analysis isn't able to determine pointer annotations, it might be acceptable if you need to manually disable this rule for@intFromPtr
calls onallowzero
pointers).For pointers that are neither optional nor
allowzero
,@intFromPtr
will never return zero because such pointers are forbidden from being zero. Optimization knows this and makes a comparison with zero always return false. So this comparison is always a bug.For optional, non-
allowzero
pointers,@intFromPtr(p) != 0
does have the intended behavior, but I think it's cleaner to writep != null
which is why I'm proposing this rule apply to those pointers too.Examples
Examples of incorrect code for this rule
Examples of correct code for this rule
The text was updated successfully, but these errors were encountered: