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 13, 2024
1 parent e162e89 commit 3c39f0f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 43 deletions.
18 changes: 9 additions & 9 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 @@ -3008,14 +3008,14 @@ declare_lint! {
///
/// 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 +3061,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 +3078,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
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ pub(crate) enum BuiltinSpecialModuleNameUsed {

#[derive(LintDiagnostic)]
#[diag(lint_builtin_return_local_variable_ptr)]
pub(crate) struct BuiltinLocalVariablePointerImpl;
pub(crate) struct BuiltinReturningPointersToLocalVariables;

// deref_into_dyn_supertrait.rs
#[derive(LintDiagnostic)]
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 3c39f0f

Please sign in to comment.