From 4e7732cc2d326ecc763b798f0f13168b4175a21a Mon Sep 17 00:00:00 2001 From: Bastian Kersting Date: Fri, 13 Dec 2024 13:12:35 +0000 Subject: [PATCH] Rename the lint and fix the tests --- compiler/rustc_lint/messages.ftl | 2 +- compiler/rustc_lint/src/builtin.rs | 26 ++++++++--------- compiler/rustc_lint/src/lib.rs | 2 +- compiler/rustc_lint/src/lints.rs | 4 +-- .../miri/tests/fail/validity/dangling_ref3.rs | 1 + .../ui/lint/lint-return-local-variable-ptr.rs | 25 ---------------- ...t-returning-pointers-to-local-variables.rs | 29 +++++++++++++++++++ ...urning-pointers-to-local-variables.stderr} | 14 ++++----- 8 files changed, 53 insertions(+), 50 deletions(-) delete mode 100644 tests/ui/lint/lint-return-local-variable-ptr.rs create mode 100644 tests/ui/lint/lint-returning-pointers-to-local-variables.rs rename tests/ui/lint/{lint-return-local-variable-ptr.stderr => lint-returning-pointers-to-local-variables.stderr} (59%) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index b907c4763e815..4231946f840f4 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -135,7 +135,7 @@ lint_builtin_overridden_symbol_name = lint_builtin_overridden_symbol_section = the program's behavior with overridden link sections on items is unpredictable and Rust cannot provide guarantees when you manually override them -lint_builtin_return_local_variable_ptr = returning a pointer to stack memory associated with a local variable +lint_builtin_returning_pointers_to_local_variables = returning a pointer to stack memory associated with a local variable lint_builtin_special_module_name_used_lib = found module declaration for lib.rs .note = lib.rs is the root of this crate's library target diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index fe3f2ddaa62cd..4b321e129448a 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -54,9 +54,9 @@ use crate::lints::{ BuiltinEllipsisInclusiveRangePatternsLint, BuiltinExplicitOutlives, BuiltinExplicitOutlivesSuggestion, BuiltinFeatureIssueNote, BuiltinIncompleteFeatures, BuiltinIncompleteFeaturesHelp, BuiltinInternalFeatures, BuiltinKeywordIdents, - BuiltinLocalVariablePointerImpl, BuiltinMissingCopyImpl, BuiltinMissingDebugImpl, - BuiltinMissingDoc, BuiltinMutablesTransmutes, BuiltinNoMangleGeneric, - BuiltinNonShorthandFieldPatterns, BuiltinSpecialModuleNameUsed, BuiltinTrivialBounds, + BuiltinMissingCopyImpl, BuiltinMissingDebugImpl, BuiltinMissingDoc, BuiltinMutablesTransmutes, + BuiltinNoMangleGeneric, BuiltinNonShorthandFieldPatterns, + BuiltinReturningPointersToLocalVariables, BuiltinSpecialModuleNameUsed, BuiltinTrivialBounds, BuiltinTypeAliasBounds, BuiltinUngatedAsyncFnTrackCaller, BuiltinUnpermittedTypeInit, BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub, BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub, BuiltinWhileTrue, InvalidAsmLabel, @@ -2987,9 +2987,9 @@ impl<'tcx> LateLintPass<'tcx> for AsmLabels { } declare_lint! { - /// The `return_local_variable_ptr` lint detects when pointer to stack - /// memory associated with a local variable is returned. That pointer - /// is immediately dangling. + /// The `returning_pointers_to_local_variables` lint detects when pointer + /// to stack memory associated with a local variable is returned. That + /// pointer is immediately dangling. /// /// ### Example /// @@ -3000,22 +3000,20 @@ declare_lint! { /// } /// ``` /// - /// This will produce: - /// /// {{produces}} /// /// ### Explanation /// /// Returning a pointer to memory refering to a local variable will always /// end up in a dangling pointer after returning. - pub RETURN_LOCAL_VARIABLE_PTR, + pub RETURNING_POINTERS_TO_LOCAL_VARIABLES, Warn, "returning a pointer to stack memory associated with a local variable", } -declare_lint_pass!(ReturnLocalVariablePointer => [RETURN_LOCAL_VARIABLE_PTR]); +declare_lint_pass!(ReturningPointersToLocalVariables => [RETURNING_POINTERS_TO_LOCAL_VARIABLES]); -impl<'tcx> LateLintPass<'tcx> for ReturnLocalVariablePointer { +impl<'tcx> LateLintPass<'tcx> for ReturningPointersToLocalVariables { fn check_fn( &mut self, cx: &LateContext<'tcx>, @@ -3061,7 +3059,7 @@ impl<'tcx> LateLintPass<'tcx> for ReturnLocalVariablePointer { } } -impl ReturnLocalVariablePointer { +impl ReturningPointersToLocalVariables { /// Evaluates the return expression of a function and emits a lint if it /// returns a pointer to a local variable. fn maybe_lint_return_expr<'tcx>(cx: &LateContext<'tcx>, return_expr: &hir::Expr<'tcx>) { @@ -3078,9 +3076,9 @@ impl ReturnLocalVariablePointer { ) = addr_expr.kind { cx.emit_span_lint( - RETURN_LOCAL_VARIABLE_PTR, + RETURNING_POINTERS_TO_LOCAL_VARIABLES, return_expr.span, - BuiltinLocalVariablePointerImpl, + BuiltinReturningPointersToLocalVariables, ); } } diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 9819d66d1240e..1f908eeb0285e 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -241,7 +241,7 @@ late_lint_methods!( IfLetRescope: IfLetRescope::default(), StaticMutRefs: StaticMutRefs, UnqualifiedLocalImports: UnqualifiedLocalImports, - ReturnLocalVariablePointer : ReturnLocalVariablePointer, + ReturningPointersToLocalVariables : ReturningPointersToLocalVariables, ] ] ); diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 1cf948d75dc44..f76f4fcdd05f2 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -532,8 +532,8 @@ pub(crate) enum BuiltinSpecialModuleNameUsed { } #[derive(LintDiagnostic)] -#[diag(lint_builtin_return_local_variable_ptr)] -pub(crate) struct BuiltinLocalVariablePointerImpl; +#[diag(lint_builtin_returning_pointers_to_local_variables)] +pub(crate) struct BuiltinReturningPointersToLocalVariables; // deref_into_dyn_supertrait.rs #[derive(LintDiagnostic)] diff --git a/src/tools/miri/tests/fail/validity/dangling_ref3.rs b/src/tools/miri/tests/fail/validity/dangling_ref3.rs index 8e8a75bd7ec04..47ab747c23327 100644 --- a/src/tools/miri/tests/fail/validity/dangling_ref3.rs +++ b/src/tools/miri/tests/fail/validity/dangling_ref3.rs @@ -1,5 +1,6 @@ // Make sure we catch this even without Stacked Borrows //@compile-flags: -Zmiri-disable-stacked-borrows +#![allow(returning_pointers_to_local_variables)] use std::mem; fn dangling() -> *const u8 { diff --git a/tests/ui/lint/lint-return-local-variable-ptr.rs b/tests/ui/lint/lint-return-local-variable-ptr.rs deleted file mode 100644 index b9958589a2150..0000000000000 --- a/tests/ui/lint/lint-return-local-variable-ptr.rs +++ /dev/null @@ -1,25 +0,0 @@ -//@ check-pass - -#![warn(return_local_variable_ptr)] - -fn foo() -> *const u32 { - let empty = 42u32; - return &empty as *const _; -} - -fn bar() -> *const u32 { - let empty = 42u32; - &empty as *const _ -} - -fn baz() -> *const u32 { - let empty = 42u32; - return ∅ -} - -fn faa() -> *const u32 { - let empty = 42u32; - &empty -} - -fn main() {} diff --git a/tests/ui/lint/lint-returning-pointers-to-local-variables.rs b/tests/ui/lint/lint-returning-pointers-to-local-variables.rs new file mode 100644 index 0000000000000..6ba8fbf637d49 --- /dev/null +++ b/tests/ui/lint/lint-returning-pointers-to-local-variables.rs @@ -0,0 +1,29 @@ +//@ check-pass + +#![warn(returning_pointers_to_local_variables)] + +fn foo() -> *const u32 { + let empty = 42u32; + return &empty as *const _; + //~^ WARN returning a pointer to stack memory associated with a local variable +} + +fn bar() -> *const u32 { + let empty = 42u32; + &empty as *const _ + //~^ WARN returning a pointer to stack memory associated with a local variable +} + +fn baz() -> *const u32 { + let empty = 42u32; + return ∅ + //~^ WARN returning a pointer to stack memory associated with a local variable +} + +fn faa() -> *const u32 { + let empty = 42u32; + &empty + //~^ WARN returning a pointer to stack memory associated with a local variable +} + +fn main() {} diff --git a/tests/ui/lint/lint-return-local-variable-ptr.stderr b/tests/ui/lint/lint-returning-pointers-to-local-variables.stderr similarity index 59% rename from tests/ui/lint/lint-return-local-variable-ptr.stderr rename to tests/ui/lint/lint-returning-pointers-to-local-variables.stderr index ab0bfc6d721db..14d65c5018611 100644 --- a/tests/ui/lint/lint-return-local-variable-ptr.stderr +++ b/tests/ui/lint/lint-returning-pointers-to-local-variables.stderr @@ -1,29 +1,29 @@ warning: returning a pointer to stack memory associated with a local variable - --> $DIR/lint-return-local-variable-ptr.rs:7:12 + --> $DIR/lint-returning-pointers-to-local-variables.rs:7:12 | LL | return &empty as *const _; | ^^^^^^^^^^^^^^^^^^ | note: the lint level is defined here - --> $DIR/lint-return-local-variable-ptr.rs:3:9 + --> $DIR/lint-returning-pointers-to-local-variables.rs:3:9 | -LL | #![warn(return_local_variable_ptr)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | #![warn(returning_pointers_to_local_variables)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: returning a pointer to stack memory associated with a local variable - --> $DIR/lint-return-local-variable-ptr.rs:12:5 + --> $DIR/lint-returning-pointers-to-local-variables.rs:13:5 | LL | &empty as *const _ | ^^^^^^^^^^^^^^^^^^ warning: returning a pointer to stack memory associated with a local variable - --> $DIR/lint-return-local-variable-ptr.rs:17:12 + --> $DIR/lint-returning-pointers-to-local-variables.rs:19:12 | LL | return ∅ | ^^^^^^ warning: returning a pointer to stack memory associated with a local variable - --> $DIR/lint-return-local-variable-ptr.rs:22:5 + --> $DIR/lint-returning-pointers-to-local-variables.rs:25:5 | LL | &empty | ^^^^^^