これは、なにをしたくて書いたもの?
Rustにはフォーマッターとリンターが付属しているようなので、とりあえず把握しておこうというだけのエントリーです。
環境
Cargoのバージョンだけ載せておきます。
$ cargo --version cargo 1.83.0 (5ffbef321 2024-10-29)
Rustfmt
フォーマッターはRustfmtを使うようです。
コマンドとしてはrustfmt
ですね。rustupをインストールした時にすでにインストールされていました。
$ rustfmt --version rustfmt 1.8.0-stable (90b35a6239 2024-11-26)
入っていなかった場合は、rustup component add rustfmt
で。
設定ファイルとしてはrustfmt.toml
または.rustfmt.toml
を使うようです。
設定項目はドキュメントを見てもよいですし、以下でもデフォルト値と合わせて確認できます。
$ rustfmt --help=config
--help
の後についている意味はこちら。
-h, --help [=TOPIC] Show this message or help about a specific topic: `config`
また、設定ファイルの雛形は以下のコマンドで作成できます。
$ rustfmt --print-config default rustfmt.toml
デフォルト、最小、カレントから選ぶことができ、指定のパスにファイルを作成します。
--print-config [default|minimal|current] PATH Dumps a default or minimal config to PATH. A minimal config is the subset of the current config file used for formatting the current program. `current` writes to stdout current config as if formatting the file at PATH.
今回の生成結果はこちら。
rustfmt.toml
max_width = 100 hard_tabs = false tab_spaces = 4 newline_style = "Auto" indent_style = "Block" use_small_heuristics = "Default" fn_call_width = 60 attr_fn_like_width = 70 struct_lit_width = 18 struct_variant_width = 35 array_width = 60 chain_width = 60 single_line_if_else_max_width = 50 single_line_let_else_max_width = 50 wrap_comments = false format_code_in_doc_comments = false doc_comment_code_block_width = 100 comment_width = 80 normalize_comments = false normalize_doc_attributes = false format_strings = false format_macro_matchers = false format_macro_bodies = true skip_macro_invocations = [] hex_literal_case = "Preserve" empty_item_single_line = true struct_lit_single_line = true fn_single_line = false where_single_line = false imports_indent = "Block" imports_layout = "Mixed" imports_granularity = "Preserve" group_imports = "Preserve" reorder_imports = true reorder_modules = true reorder_impl_items = false type_punctuation_density = "Wide" space_before_colon = false space_after_colon = true spaces_around_ranges = false binop_separator = "Front" remove_nested_parens = true combine_control_expr = true short_array_element_width_threshold = 10 overflow_delimited_expr = false struct_field_align_threshold = 0 enum_discrim_align_threshold = 0 match_arm_blocks = true match_arm_leading_pipes = "Never" force_multiline_blocks = false fn_params_layout = "Tall" brace_style = "SameLineWhere" control_brace_style = "AlwaysSameLine" trailing_semicolon = true trailing_comma = "Vertical" match_block_trailing_comma = false blank_lines_upper_bound = 1 blank_lines_lower_bound = 0 edition = "2015" style_edition = "2015" version = "One" inline_attribute_width = 0 format_generated_files = true generated_marker_line_search_limit = 5 merge_derives = true use_try_shorthand = false use_field_init_shorthand = false force_explicit_abi = true condense_wildcard_suffixes = false color = "Auto" required_version = "1.8.0" unstable_features = false disable_all_formatting = false skip_children = false show_parse_errors = true error_on_line_overflow = false error_on_unformatted = false ignore = [] emit_mode = "Files" make_backup = false
なのですが、そのままcargo fmt
を実行すると大量にWarningが出たので最初は設定ファイルなしでいこうと思います…。
実行ですが、rustfmt
コマンドの場合はファイル指定で行います。
$ rustfmt src/main.rs
これでソースコードがフォーマットされます。
ですがrustfmt
はファイル単位のみの指定のようなので、実際にはcargo fmt
で行うことになると思います。これでプロジェクト内の
ソースコードがフォーマットされるようです。
$ cargo fmt
cargo fmt
は、Cargoのドキュメントには書かれていないのですが…。
cargo --list
で表示すると現れます。
$ cargo --list
こちらですね。
fmt Formats all bin and lib files of the current crate using rustfmt.
ヘルプ。
$ cargo fmt --help This utility formats all bin and lib files of the current crate using rustfmt. Usage: cargo fmt [OPTIONS] [-- <rustfmt_options>...] Arguments: [rustfmt_options]... Options passed to rustfmt Options: -q, --quiet No output printed to stdout -v, --verbose Use verbose output --version Print rustfmt version and exit -p, --package <package>... Specify package to format --manifest-path <manifest-path> Specify path to Cargo.toml --message-format <message-format> Specify message-format: short|json|human --all Format all packages, and also their local path-based dependencies --check Run rustfmt in check mode -h, --help Print help
チェックだけ行う場合はこちら。
$ cargo fmt --check
結果を詳細表示する場合はこちら。
$ cargo fmt -v
Clippy
リンターはClippyを使うようです。
Introduction - Clippy Documentation
インストール方法はこちら。ですが、rustupをインストールした時に含まれていそうですね。
Installation - Clippy Documentation
コマンドを直接使う場合は、clippy-driver
になります。
$ clippy-driver --version clippy 0.1.83 (90b35a6 2024-11-26)
実際にはcargo clippy
で実行することになるでしょう。
$ cargo clippy
例によってCargoの単純な--help
では表示されないので、cargo --list
で存在を確認できます。
clippy Checks a package to catch common mistakes and improve your Rust code.
設定ファイルはclippy.toml
または.clippy.toml
として作成します。
Configuration - Clippy Documentation
設定可能な項目はこちらです。
Lint Configuration - Clippy Documentation
おわりに
RustのフォーマッターであるRustfmt、リンターであるClippyについて軽く調べてみました。
とりあえず使っていかないと覚えないので、まずは存在の把握からということで。