{"id":18894101,"url":"https://github.com/trailofbits/cast_checks","last_synced_at":"2025-04-15T00:31:54.233Z","repository":{"id":147013750,"uuid":"615877199","full_name":"trailofbits/cast_checks","owner":"trailofbits","description":"A procedural macro to check for invalid casts","archived":false,"fork":false,"pushed_at":"2025-03-03T16:31:29.000Z","size":46,"stargazers_count":8,"open_issues_count":0,"forks_count":3,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-28T12:51:18.403Z","etag":null,"topics":["casting","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trailofbits.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-18T23:44:53.000Z","updated_at":"2025-03-27T11:01:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"642d859f-9a18-4431-a423-2d4ee806b2f6","html_url":"https://github.com/trailofbits/cast_checks","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailofbits%2Fcast_checks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailofbits%2Fcast_checks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailofbits%2Fcast_checks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trailofbits%2Fcast_checks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trailofbits","download_url":"https://codeload.github.com/trailofbits/cast_checks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248984387,"owners_count":21193738,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["casting","rust"],"created_at":"2024-11-08T08:17:56.891Z","updated_at":"2025-04-15T00:31:53.693Z","avatar_url":"https://github.com/trailofbits.png","language":"Rust","readme":"# `cast_checks`\n\nA procedural macro to check for invalid casts\n\nLike [`-C overflow-checks`], `cast_checks` is enabled only for debug builds by default. To enable `cast_checks` for release builds, set the crate-level `release` feature.\n\n## How it works\n\n`cast_checks::enable` essentially rewrites each expression of the form:\n\n```rust,ignore\nexpr as T\n```\n\nto an expression involving [`try_into`]:\n\n```rust,ignore\n\u003c_ as TryInto::\u003c T \u003e\u003e::try_into( expr ).expect(\"invalid cast\")\n```\n\nSo when an invalid cast occurs, a message like the following results:\n\n```text\nthread 'checked_truncation' panicked at 'invalid cast: TryFromIntError(())', cast_checks/tests/basic.rs:30:13\n```\n\nWe say \"essentially rewrites\" because the actual generated code is slightly more complex. It uses [Nikolai Vazquez]'s [`impls`]' [trick] to determine whether an appropriate [`TryInto`] implementation exists.\n\n## How to use\n\n### With a stable compiler\n\nYou must use `cast_checks::enable` as an outer [attribute]. Example:\n\n```rust\n#[cast_checks::enable]\nfn as_u16(x: u64) -\u003e u16 {\n    x as u16\n}\n```\n\n### With a nightly compiler\n\n**We recommend enabling Rust features [`custom_inner_attributes`] and [`proc_macro_hygiene`].**\n\nIf you enable the [`custom_inner_attributes`] and [`proc_macro_hygiene`] features, you can use `cast_checks::enable` as an inner [attribute]. Example:\n\n```rust,ignore\n#![feature(custom_inner_attributes, proc_macro_hygiene)]\n\nmod m {\n    #![cast_checks::enable]\n\n    /* items */\n}\n```\n\n## `CAST_CHECKS_LOG`\n\nIf you are concerned that some casts are not being checked, try setting `CAST_CHECKS_LOG` and passing the [`procmacro2_semver_exempt`] config flag when compiling, e.g.:\n\n```sh\nCAST_CHECKS_LOG=1 RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build\n```\n\nThis will cause `cast_checks` to dump to standard output:\n\n- all rewritten locations\n- all modules whose contents are not visited because they are not inlined\n\nExample:\n\n```text\ncast_checks rewriting `x as u16` at src/lib.rs:3:0\ncast_checks not descending into `mod c ;` at src/lib.rs:3:0\n```\n\nNote that `CAST_CHECKS_LOG` requires `--cfg procmacro2_semver_exempt` to be passed to rustc.\n\n[`-c overflow-checks`]: https://doc.rust-lang.org/rustc/codegen-options/index.html#overflow-checks\n[attribute]: https://doc.rust-lang.org/reference/attributes.html\n[`custom_inner_attributes`]: https://github.com/rust-lang/rust/issues/54726\n[`procmacro2_semver_exempt`]: https://github.com/dtolnay/proc-macro2#unstable-features\n[`proc_macro_hygiene`]: https://github.com/rust-lang/rust/issues/54727\n[`rustflags='--cfg procmacro2_semver_exempt'`]: https://github.com/dtolnay/proc-macro2#unstable-features\n[nikolai vazquez]: https://github.com/nvzqz\n[`impls`]: https://github.com/nvzqz/impls\n[trick]: https://github.com/nvzqz/impls#how-it-works\n[`tryinto`]: https://doc.rust-lang.org/std/convert/trait.TryInto.html\n[`try_into`]: https://doc.rust-lang.org/std/convert/trait.TryInto.html#tymethod.try_into\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrailofbits%2Fcast_checks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrailofbits%2Fcast_checks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrailofbits%2Fcast_checks/lists"}