Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 24, 2025

Adds catalog rule to detect Rust functions marked unsafe that contain no unsafe blocks—indicating either unnecessary unsafe marking or missing explicit unsafe block wrapping.

Changes

  • Created website/catalog/rust/redundant-unsafe-function.md with YAML rule configuration
  • Added rule to Rust catalog index
  • Includes playground link with rule pre-configured

Rule Coverage

Detects:

  • Unsafe functions without return types
  • Unsafe async/const functions
  • Functions with complex return types

Correctly excludes:

  • Functions with proper unsafe blocks
  • Regular non-unsafe functions
// Matches - no unsafe block inside
unsafe fn redundant_unsafe() {
    println!("No unsafe operations here");
}

// Does not match - has unsafe block
unsafe fn proper_unsafe() -> *const i32 {
    unsafe {
        let ptr = 0x1234 as *const i32;
        ptr
    }
}
Original prompt

This section details on the original issue you should resolve

<issue_title>[example] mark unsafe function without unsafe block</issue_title>
<issue_description>https://x.com/hd_nvim/status/1992810384072585397?s=20

https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InJ1c3QiLCJxdWVyeSI6IntcbiAgZGVzY3JpcHRpb24gPSAkQVxufSIsInJld3JpdGUiOiIiLCJzdHJpY3RuZXNzIjoic21hcnQiLCJzZWxlY3RvciI6ImJpbmRpbmciLCJjb25maWciOiIgIGlkOiByZWR1bmRhbnQtdW5zYWZlLWZ1bmN0aW9uXG4gIGxhbmd1YWdlOiBydXN0XG4gIHNldmVyaXR5OiBlcnJvclxuICBtZXNzYWdlOiBVbnNhZmUgZnVuY3Rpb24gd2l0aG91dCB1bnNhZmUgYmxvY2sgaW5zaWRlXG4gIG5vdGU6IHxcbiAgICBDb25zaWRlciB3aGV0aGVyIHRoaXMgZnVuY3Rpb24gbmVlZHMgdG8gYmUgbWFya2VkIHVuc2FmZSBcbiAgICBvciBpZiB1bnNhZmUgb3BlcmF0aW9ucyBzaG91bGQgYmUgd3JhcHBlZCBpbiBhbiB1bnNhZmUgYmxvY2tcbiAgcnVsZTpcbiAgICBhbGw6XG4gICAgICAtIGtpb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AgICB1bnNhZmUge1xuICAgICAgICAgIGxldCBwdHIgPSAweDEyMzQgYXMgKmNvbnN0IGkzMjtcbiAgICAgICAgICBwdHJcbiAgICAgIH1cbiAgfVxuXG4gIC8vIFNob3VsZCBtYXRjaCAtIHVuc2FmZSBhc3luYyBmdW5jdGlvbiB3aXRob3V0IHVuc2FmZSBibG9ja1xuICB1bnNhZmUgYXN5bmMgZm4gYXN5bmNfcmVkdW5kYW50KCkgLT4gaTMyIHtcbiAgICAgIDQyXG4gIH1cblxuICAvLyBTaG91bGQgbWF0Y2ggLSB1bnNhZmUgY29uc3QgZnVuY3Rpb25cbiAgdW5zYWZlIGNvbnN0IGZuIGNvbnN0X3JlZHVuZGFudCgpIC0+IGkzMiB7XG4gICAgICAxMDBcbiAgfVxuXG4gIC8vIFNob3VsZCBOT1QgbWF0Y2ggLSByZWd1bGFyIGZ1bmN0aW9uXG4gIGZuIHJlZ3VsYXJfZnVuY3Rpb24oKSAtPiBpMzIge1xuICAgICAgNDJcbiAgfSJ9

  id: redundant-unsafe-function
  language: rust
  severity: error
  message: Unsafe function without unsafe block inside
  note: |
    Consider whether this function needs to be marked unsafe 
    or if unsafe operations should be wrapped in an unsafe block
  rule:
    all:
      - kind: function_item
      - has:
          kind: function_modifiers
          regex: "^unsafe"
      - not:
          has:
            kind: unsafe_block
            stopBy: end
  // Should match - unsafe function without unsafe block (no return type)
  unsafe fn redundant_unsafe() {
      println!("No unsafe operations here");
  }

  // Should match - unsafe function with return type, no unsafe block
  unsafe fn redundant_with_return() -> i32 {
      let x = 5;
      x + 10
  }

  // Should match - unsafe function with complex return type
  unsafe fn redundant_complex_return() -> Result<String, std::io::Error> {
      Ok(String::from("safe operation"))
  }

  // Should NOT match - unsafe function with unsafe block
  unsafe fn proper_unsafe() -> *const i32 {
      unsafe {
          let ptr = 0x1234 as *const i32;
          ptr
      }
  }

  // Should match - unsafe async function without unsafe block
  unsafe async fn async_redundant() -> i32 {
      42
  }

  // Should match - unsafe const function
  unsafe const fn const_redundant() -> i32 {
      100
  }

  // Should NOT match - regular function
  fn regular_function() -> i32 {
      42
  }
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Nov 24, 2025
@coderabbitai
Copy link

coderabbitai bot commented Nov 24, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Tip

📝 Customizable high-level summaries are now available in beta!

You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later.


Comment @coderabbitai help to get the list of available commands and usage tips.

Co-authored-by: HerringtonDarkholme <2883231+HerringtonDarkholme@users.noreply.github.com>
Copilot AI changed the title [WIP] Mark unsafe function without unsafe block Add Rust rule for detecting unsafe functions without unsafe blocks Nov 24, 2025
Copilot finished work on behalf of HerringtonDarkholme November 24, 2025 04:34
@HerringtonDarkholme HerringtonDarkholme marked this pull request as ready for review November 24, 2025 05:16
@HerringtonDarkholme HerringtonDarkholme added this pull request to the merge queue Nov 24, 2025
Merged via the queue into main with commit 81d8996 Nov 24, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[example] mark unsafe function without unsafe block

2 participants