Skip to content

Commit

Permalink
Rename the lint and fix the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1c3t3a committed Dec 14, 2024
1 parent e162e89 commit 4e7732c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 50 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 12 additions & 14 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
///
Expand All @@ -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>,
Expand Down Expand Up @@ -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>) {
Expand All @@ -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,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ late_lint_methods!(
IfLetRescope: IfLetRescope::default(),
StaticMutRefs: StaticMutRefs,
UnqualifiedLocalImports: UnqualifiedLocalImports,
ReturnLocalVariablePointer : ReturnLocalVariablePointer,
ReturningPointersToLocalVariables : ReturningPointersToLocalVariables,
]
]
);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/fail/validity/dangling_ref3.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
25 changes: 0 additions & 25 deletions tests/ui/lint/lint-return-local-variable-ptr.rs

This file was deleted.

29 changes: 29 additions & 0 deletions tests/ui/lint/lint-returning-pointers-to-local-variables.rs
Original file line number Diff line number Diff line change
@@ -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 &empty;
//~^ 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() {}
Original file line number Diff line number Diff line change
@@ -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 &empty;
| ^^^^^^

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
| ^^^^^^
Expand Down

0 comments on commit 4e7732c

Please sign in to comment.