All skills
apollographql avatar

/rust-best-practices

@13ff457 official
by Apollo GraphQLapollographql/skills115 stars
13

Guide for writing idiomatic Rust code based on Apollo GraphQL's best practices handbook. Use this skill when: (1) writing new Rust code or functions, (2) reviewing or refactoring existing Rust code, (3) deciding between borrowing vs cloning or ownership patterns, (4) implementing error handling with Result types, (5) optimizing Rust code for performance, (6) writing tests or documentation for Rust projects.

Use this Skill: https://skilld.dev/gh/apollographql/skills/rust-best-practices

This session only. Nothing lands on disk.

referenceschapter_02.md

≈1.4k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Chapter 2 - Clippy and Linting Discipline

Be sure to have cargo clippy installed with your rust compiler, run cargo clippy -V in your terminal for a rust project and you should get something like this clippy 0.1.86 (05f9846f89 2025-03-31). If terminal fails to show a clippy version, please run the following code rustup update && rustup component add clippy.

Clippy documentation can be found here.

2.1 Why care about linting?

Rust compiler is a powerful tool that catches many mistakes. However, some more in-depth analysis require extra tools, that is where cargo clippy comes into play. Clippy checks for:

  • Performance pitfalls.
  • Style issues.
  • Redundant code.
  • Potential bugs.
  • Non-idiomatic Rust.

2.2 Always run cargo clippy

Add the following to your daily workflow:

$ cargo clippy --all-targets --all-features --locked -- -D warnings
  • --all-targets: checks library, tests, benches and examples.
  • --all-features: checks code with all features enabled; it does not auto-solve conflicting features.
  • --locked: Requires Cargo.lock to be up-to-date, can be solved with $ cargo update.
  • -D warnings: treats warnings as errors

Potential additional elements to add:

  • -- -W clippy::pedantic: lints which are rather strict or have occasional false positives.
  • -- -W clippy::nursery: Optionally can be added to check for new lints that are still under development.
  • ❗ Add this to your Makefile, Justfile, xtask or CI Pipeline.

Example at ApolloGraphQL

In the Router project there is a xtask configured for linting that can be executed with cargo xtask lint.

2.3 Important Clippy Lints to Respect

Lint Name Why Link
redundant_clone Detects unnecessary clones, has performance impact link (nursery + perf)
needless_borrow group Removes redundant & borrowing link (style)
map_unwrap_or / map_or Simplifies nested Option/Result handling map_unwrap_or unnecessary_map_or unnecessary_result_map_or_else
manual_ok_or Suggest using .ok_or_else instead of match link (style)
large_enum_variant Warns if an enum has very large variant which is bad for memory. Suggests Boxing it link (perf)
unnecessary_wraps If your function always returns Some or Ok, you don't need Option/Result link (pedantic)
clone_on_copy Catches accidental .clone() on Copy types like u32 and bool link (complexity)
needless_collect Prevents collecting and allocating an iterator, when allocation is not needed link (nursery)

2.4 Fix warnings, don't silence them!

NEVER just #[allow(clippy::lint_something)] unless:

  • You truly understand why the warning happens and you have a reason why it is better that way.
  • You document why it is being ignored.
  • ❗ Don't use allow, but expect, it will give a warning in case the lint is not true anymore, #[expect(clippy::lint_something)].

Example:

// Faster matching is preferred over size efficiency
#[expect(clippy::large_enum_variant)]
enum Message {
    Code(u8),
    Content([u8; 1024]),
}

The fix would be:

// Faster matching is preferred over size efficiency
#[expect(clippy::large_enum_variant)]
enum Message {
    Code(u8),
    Content(Box<[u8; 1024]>),
}

Handling false positives

Sometimes Clippy complains even when your code is correct, in those cases there are two solutions:

  1. Try to refactor the code, so it improves the warning.
  2. Locally override the lint with #[expect(clippy::lint_name)] and a comment with the reason.
  3. Avoid global overrides, unless it is core crate issue, a good example of this is the Bevy Engine that has a set of lints that should be allowed by default.

2.5 Configure workspace/package lints

In your Cargo.toml file it is possible to determine which lints and their priorities over each other. In case of 2 or more conflicting lints, the higher priority one will be chosen. Example configuration for a package:

[lints.rust]
future-incompatible = "warn"
nonstandard_style = "deny"

[lints.clippy]
all = { level = "deny", priority = 10 }
redundant_clone = { level = "deny", priority = 9 }
manual_while_let_some = { level = "deny", priority = 4 }
pedantic = { level = "warn", priority = 3 }

And for a workspace:

[workspace.lints.rust]
future-incompatible = "warn"
nonstandard_style = "deny"

[workspace.lints.clippy]
all = { level = "deny", priority = 10 }
redundant_clone = { level = "deny", priority = 9 }
manual_while_let_some = { level = "deny", priority = 4 }
pedantic = { level = "warn", priority = 3 }

Source: SKILL.md on GitHub

No alerts3d5 checks · Risk SAFE
  • Gen Agent Trust Hub3d

    This skill provides a comprehensive guide to idiomatic Rust development based on Apollo GraphQL's best practices. It covers coding styles, error handling, performance optimization, and testing procedures using standard ecosystem tools.

  • Socket3d

    No alerts

  • Snyk3d

    Risk: LOW · No issues

  • Runlayer6mo

    1/10 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 13ff457. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 3 days ago
What it can do
Runs commands
compatibility
Rust 1.70+, Cargo
metadata
{
  "author": "apollographql",
  "version": "1.1.2"
}
All 1 allowed tools
Bash(cargo:*) Bash(rustc:*) Bash(rustfmt:*) Bash(clippy:*) Read Write Edit Glob Grep

README badge

README badge for apollographql/skills/rust-best-practices

Guides idiomatic Rust code review and writing using Apollo GraphQL's best practices handbook, covering ownership patterns, error handling, performance profiling, testing conventions, and type-state patterns. Use when writing new Rust, refactoring existing code, deciding between borrowing vs cloning, or implementing error handling and performance optimizations.

Generated from the current SKILL.md.

Does this skill cover async Rust patterns?
The skill references Apollo's best practices handbook which covers core ownership, borrowing, error handling, testing, and type patterns, but async-specific guidance is not listed in the chapter references provided.
Can I use this skill to lint my existing codebase?
Yes. The skill includes Clippy configuration and linting best practices, with specific commands like `cargo clippy --all-targets --all-features --locked -- -D warnings` to identify issues in your code.
Does this skill recommend anyhow or thiserror for error handling?
Yes. The skill specifies using `thiserror` for library errors and `anyhow` for binaries only, and advises never using `unwrap()` or `expect()` outside tests.
What Rust version does this require?
The skill requires Rust 1.70 or later and Cargo.
Does this cover the type state pattern?
Yes. Chapter 7 covers the type state pattern for encoding valid states in the type system to catch invalid operations at compile time, with a code example showing Connection states.

Generated from the current SKILL.md. These answers refresh after source changes.