Skip to main content
rule

fortran — Cursor rule: what it does, how to install

A Cursor rule for modern Fortran practices covering modules, types, arrays, builds, and tests. Use it when working on scientific Fortran code.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/fortran.mdc
Open on GitHub

What fortran does

This rule guides Cursor toward Fortran 2003, 2008, or newer. It asks for implicit none, explicit interfaces through modules, shared kind parameters, descriptive names, and structured control flow. Legacy patterns such as COMMON blocks, numeric labels, and GOTO-heavy code are discouraged.

Procedures should declare intent(in), intent(out), or intent(inout) for every dummy argument, while module imports should use use, only:. The guidance favors allocatable arrays, checks allocation state and dimensions, and avoids needless allocation inside hot loops. It also covers tests for public procedures, invalid input, boundary cases, numerical tolerances, and builds with CMake, fpm, or Make.

The rule targets Fortran source files plus CMakeLists.txt, *.cmake, and Makefile. Its alwaysApply value is false, so files outside those glob patterns will not receive it automatically.

What it helps with

  • organize Fortran code with focused modules
  • define shared numeric kind parameters
  • check procedure intents and explicit interfaces
  • prevent unsafe allocatable-array usage
  • test numerical boundary cases

Example prompts for the agent

  • Rewrite this procedure in modern Fortran and add explicit intent declarations for its arguments.
  • Review solver.f90 for type, array, and module-import problems.
  • Add tests for invalid inputs and boundary conditions, then check numerical tolerances in the CMakeLists.txt build.

How to install fortran

Manually: put the files in place

.cursor/rules/fortran.mdc

Frontmatter: description, globs, alwaysApply

Step by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules

Questions about fortran

How do I install fortran in Cursor?

Save the rule in the project file .cursor/rules/fortran.mdc.

What does the fortran rule do?

It guides Cursor on modern Fortran modules, explicit interfaces, kind parameters, memory handling, testing, and build configuration.

Which files does the fortran rule target?

It targets .f, .f90, .f95, .f03, .f08, .for, and .ftn files, plus CMakeLists.txt, *.cmake, and Makefile.

Source text

fortran.mdc · CC0-1.0
---
description: Modern Fortran rules for scientific computing, modules, explicit interfaces, kind parameters, memory safety, and testing
globs: ["**/*.f", "**/*.f90", "**/*.f95", "**/*.f03", "**/*.f08", "**/*.for", "**/*.ftn", "CMakeLists.txt", "*.cmake", "Makefile"]
alwaysApply: false
---

# Fortran Programming Guidelines

## Basic Principles

- Use modern Fortran standards such as Fortran 2003, 2008, or newer.
- Use `implicit none` in every program unit.
- Put procedures in modules to provide explicit interfaces.
- Keep modules focused and place each major module in its own file.
- Prefer clear, structured code over clever language tricks.
- Avoid obsolete features such as COMMON blocks, GOTO-heavy control flow, and numeric labels.

## Kinds and Types

- Define numeric kind parameters in one shared module, such as `kind_mod`.
- Use `real(kind=dp)` or the project-approved real kind for floating point values.
- Use `integer(kind=i4)` or the project-approved integer kind for integer values.
- Define constants such as pi explicitly.
- Include units in comments for physical quantities.
- Use derived types to group related data instead of passing many primitive arguments.

## Naming and Style

- Use lowercase for language keywords and most identifiers.
- Use underscores for multi-word names.
- Avoid names that differ only by case.
- Use descriptive names for procedures and state.
- Repeat the procedure or module name after `end` statements.
- Keep indentation consistent in `do`, `if`, `select case`, and module blocks.

## Procedures

- Keep subroutines and functions short and single-purpose.
- Use `intent(in)`, `intent(out)`, or `intent(inout)` for every dummy argument.
- Keep functions free of side effects whenever possible.
- Prefer early validation and clear returns over deep nesting.
- Use `use, only:` when importing from modules.

## Memory and Arrays

- Prefer allocatable arrays over pointers unless pointer semantics are required.
- Check allocation state and array sizes before use.
- Deallocate allocatable arrays when their lifetime is not naturally scoped.
- Specify array bounds clearly when they matter.
- Avoid unnecessary dynamic allocation in hot loops.

## Testing and Build

- Use CMake, fpm, Make, or the project-standard build system consistently.
- Compile with warnings enabled and treat important warnings as failures in CI.
- Add unit tests for public procedures and integration tests for numerical workflows.
- Test boundary conditions, invalid inputs, and representative scientific cases.
- Verify numerical tolerances explicitly rather than relying on exact floating point equality.

## Common Mistakes

- Do not declare variables after executable code unless using a block construct.
- Do not assume `random_number` is a function; it is a subroutine.
- Do not write to stdout from pure procedures.
- Do not declare the same variable twice in the same scope.
- Do not assume pi, dp, or project kinds already exist without importing or defining them.

© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules

Similar IDE rules

All IDE rules for Programming languages →

From the same set210

gitflow Configures Gitflow branching standards, Conventional Commits format, and release steps for the Cursor assistant. ★ 41,024repo stars git-conventional-commit-messages This Cursor rule shapes Git commit messages around Conventional Commits. Use it when an agent needs consistent types, scopes, bodies, and footers. ★ 41,024repo stars gherkin-style-testing-cursorrules-prompt-file A Cursor rule that turns technical test scripts and manual cases into Gherkin scenarios written in plain business language. ★ 41,024repo stars gamemaker-gml Guides Cursor with GameMaker Language conventions for objects, scripts, and rooms. Keeps GML projects performant and prevents common memory leaks. ★ 41,024repo stars flutter-development-guidelines-cursorrules-prompt-file Locks down folder layout, naming and Dart style for Flutter apps built on MVVM with Riverpod. Useful when the agent scatters files across the project. ★ 41,024repo stars flutter-app-expert-cursorrules-prompt-file Configures Cursor for Flutter 3.x development with Clean Architecture, BLoC, and Material 3 design. Guides code structure and testing practices. ★ 41,024repo stars fastapi A Cursor rule for FastAPI work in Python. It guides project structure, API design, models, security, database code, testing, and deployment. ★ 41,024repo stars es-module-nodejs-guidelines-cursorrules-prompt-fil Cursor rule for Node.js development with ES module syntax. It enforces short responses, prioritizes task steps, and controls verbosity via Vx flags. ★ 41,024repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor