Skip to content

Improve as guideline (gui_ADHABsmK9FXz)#426

Open
inkreasing wants to merge 1 commit intorustfoundation:mainfrom
inkreasing:fix_as
Open

Improve as guideline (gui_ADHABsmK9FXz)#426
inkreasing wants to merge 1 commit intorustfoundation:mainfrom
inkreasing:fix_as

Conversation

@inkreasing
Copy link

@inkreasing inkreasing commented Mar 10, 2026

should help with #338.

I don't know if the state of the guideline is "good" after this PR, but at least it's not indirectly recommending doing UB anymore.

@netlify
Copy link

netlify bot commented Mar 10, 2026

Deploy Preview for scrc-coding-guidelines failed.

Name Link
🔨 Latest commit e3b004c
🔍 Latest deploy log https://app.netlify.com/projects/scrc-coding-guidelines/deploys/69b08817e85c8900087b6919


**Exception:** ``as`` may be used with ``usize`` as the right operand and an expression of raw pointer
type as the left operand.
**Exception:** ``as`` may be used with an integer type as the right operand and an expression of floating
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i added this exception because there is no way to go from float to int otherwise. If you want i can also remove this.

A pointer-to-address cast does not lose value, but will be truncated unless the destination type is large
enough to hold the address value. The ``usize`` type is guaranteed to be wide enough for this purpose.
A pointer-to-address or address-to-pointer cast should be performed using the exposed or strict provenance APIs
(``addr``, ``expose_provenance``, ``with_addr`` or ``with_exposed_provenance``).
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would like to link to guideline that explains exposed and strict provenance and recommends one of them (probably strict). Depending on how strongly you want to discourage exposed i can also remove the suggestion here.

let _a1 = p1 as usize; // compliant by exception
let _a2 = p1 as u16; // non-compliant - may lose address range
let _a3 = p1 as u64; // non-compliant - use usize to indicate intent
let _a3 = p1 as u64; // non-compliant - use .addr() or .expose_provenance()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also remove exposed provance suggestion if you want

Valid conversions that risk losing value, where doing so would be an error, can
communicate this and include an error check, with ``try_into`` or ``try_from``.
Other forms of conversion may find ``transmute`` better communicates their intent.
Other forms of conversion may find explicit functions better communicate their intent.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sentence is really general now, but there are a lot of methods depending on the type and the conversion that should be done.

// does something entirely different,
// reinterpreting the bits of z as the IEEE bit pattern of a double
// precision object, rather than converting the integer value
let _f1: f64 = _z.to_bits();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know if this example is useful. It doesn't replace an as it does something completely different. But it shouldn't use transmute

@inkreasing
Copy link
Author

Is the CI failure actually because of this PR or is it #425?

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