rust/src/tools
Stuart Cook 2a947a0efa
Rollup merge of #144322 - Urgau:dangling-ptr-from-locals, r=oli-obk
Add lint against dangling pointers from local variables

## `dangling_pointers_from_locals`

*warn-by-default*

The `dangling_pointers_from_locals` lint detects getting a pointer to data of a local that will be dropped at the end of the function.

### Example

```rust
fn f() -> *const u8 {
    let x = 0;
    &x // returns a dangling ptr to `x`
}
```

```text
warning: a dangling pointer will be produced because the local variable `x` will be dropped
  --> $DIR/dangling-pointers-from-locals.rs:10:5
   |
LL | fn simple() -> *const u8 {
   |                --------- return type of the function is `*const u8`
LL |     let x = 0;
   |         - `x` is defined inside the function and will be drop at the end of the function
LL |     &x
   |     ^^
   |
   = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function because nothing is referencing it as far as the type system is concerned
   = note: `#[warn(dangling_pointers_from_locals)]` on by default
```

### Explanation

Returning a pointer from a local value will not prolong its lifetime, which means that the value can be dropped and the allocation freed while the pointer still exists, making the pointer dangling.

If you need stronger guarantees, consider using references instead, as they are statically verified by the borrow-checker to never dangle.

------

This is related to GitHub codeql [CWE-825](https://github.com/github/codeql/blob/main/rust/ql/src/queries/security/CWE-825/AccessAfterLifetimeBad.rs) which shows examples of such simple miss-use.

It should be noted that C compilers warns against such patterns even without `-Wall`, https://godbolt.org/z/P7z98arrc.

------

`@rustbot` labels +I-lang-nominated +T-lang
cc `@traviscross`
r? compiler
2025-08-04 11:24:36 +10:00
..
build-manifest Add windows-gnullvm hosts to the manifest 2025-06-26 01:42:44 +02:00
bump-stage0 Remove git_repository field from GitConfig 2025-04-23 10:41:20 +02:00
cargo@840b83a10f Update cargo 2025-07-31 10:03:06 -04:00
cargotest
clippy Rollup merge of #144691 - xizheyin:suggest-confuse, r=estebank 2025-08-01 00:38:20 -04:00
collect-license-metadata
compiletest Rollup merge of #144805 - Zalathar:proc-res, r=jieyouxu 2025-08-03 21:56:57 +02:00
coverage-dump Update miniz_oxide dependency of coverage_dump 2025-05-10 18:35:20 +02:00
enzyme@2cccfba93c enzyme submodule update 2025-07-18 16:23:47 -07:00
error_index_generator
features-status-dump Remove backtrace dep from anyhow in features status dump tool 2025-05-01 07:45:47 -04:00
generate-copyright bump cargo_metadata 2025-07-27 12:47:39 +03:00
generate-windows-sys
html-checker
jsondocck Compiletest: Simplify {Html,Json}DocCk directive handling 2025-07-13 16:00:23 +02:00
jsondoclint rustdoc_json: represent generic args consistently. 2025-06-21 13:52:46 +10:00
libcxx-version
linkchecker add --link-targets-dir flag to linkchecker 2025-07-28 11:23:11 +02:00
lint-docs Rollup merge of #143631 - hkBst:update-escaper-2, r=compiler-errors 2025-07-17 10:41:45 +02:00
lld-wrapper
llvm-bitcode-linker
miri Allow dangling_pointers_from_locals lint in tests 2025-07-31 22:35:22 +02:00
miropt-test-tools miropt: move to edition 2024 2025-07-22 15:02:34 +00:00
nix-dev-shell Deduplicate nix code 2025-04-17 00:01:04 -04:00
opt-dist Update codegen_{cranelift,gcc} and opt-dist to use build.compiletest-allow-stage0 2025-07-30 19:55:07 +08:00
remote-test-client remote-test-client: Exit code 128 + <signal-number> instead of 3 2025-07-04 20:44:26 +02:00
remote-test-server
replace-version-placeholder
run-make-support add correct dynamic_lib_extension for aix 2025-07-31 15:49:23 -04:00
rust-analyzer Merge pull request #20321 from rust-lang/rustc-pull 2025-07-28 17:42:46 +00:00
rust-installer Update README.md 2025-06-29 12:29:28 +03:00
rustbook cargo update 2025-08-03 00:27:18 +00:00
rustc-perf@dde879cf10 Update rustc-perf submodule 2025-07-29 16:18:52 +02:00
rustdoc
rustdoc-gui Update browser-ui-test version to 0.21.1 2025-07-03 18:08:23 +02:00
rustdoc-gui-test integrate build_helper::npm into js checks and package.json usage 2025-07-19 14:56:42 -05:00
rustdoc-js Update rustdoc search tester to new alias output 2025-07-16 14:27:22 +02:00
rustdoc-themes
rustfmt parse const trait Trait 2025-07-17 18:06:26 +08:00
test-float-parse Remove uncessary parens in closure body with unused lint 2025-07-10 09:25:56 +08:00
tidy Rollup merge of #144042 - dpaoliello:verifyllvmcomp, r=jieyouxu 2025-07-30 17:59:37 +10:00
tier-check tiercheck: edition 2024 2025-07-14 08:30:27 +00:00
unicode-table-generator unicode-table-gen: more clippy fixes 2025-07-18 15:03:43 +00:00
unstable-book-gen Rollup merge of #141526 - jyn514:env-vars, r=petrochenkov 2025-05-27 01:29:21 +08:00
wasm-component-ld Update wasm-component-ld to 0.5.14 2025-06-24 15:59:35 -07:00
x x: use let-else 2025-07-11 05:38:05 +00:00
cherry-pick.sh
publish_toolstate.py