-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make use of the crate's prelude to replace individual imports
Automatically apply changes with the following: #!/bin/bash set -eux files=() # Types either defined in this crate or in `core` prelude_types=( c_char c_double c_float c_int c_longlong c_long c_short c_uchar c_uint c_ulonglong c_ulong c_ushort c_void intptr_t size_t ssize_t Clone Copy Option Send Sync ) # Reexports from core prelude_modules=( fmt hash iter mem ) # Everything in the prelude prelude=( "${prelude_types[@]}" "${prelude_modules[@]}" ) # Generate a list of all files excluding `lib.rs` (since the prelude being # defined there makes string matching weird). while IFS= read -r -d '' file; do files+=("$file") done < <(find src -name '*.rs' -not -name '*lib.rs' -not -name '*macros.rs' -not -name 'fixed_width_ints.rs' -print0) for file in "${files[@]}"; do needs_prelude=0 # If the file already has some sort of glob import, skip it if rg --pcre2 -q 'use (crate|super)::(?!prelude).*\*' "$file"; then continue fi # Core types always require the prelude to handle rustc-dep-of-std if rg --pcre2 -q '\b(?<!\.)(Option|Clone|Copy|Send|Sync|fmt|hash|iter|mem)\b' "$file"; then needs_prelude=1 fi # If we use any types that are specified in the prelude then we will import it for ty in "${prelude[@]}"; do # If the type is defined in the current module, we don't need it from the prelude if rg -q "type $ty =" "$file"; then continue fi if rg -qU '((crate|super)::'"$ty"'|use (crate|super)::(\{\n){0,2}.*'"$ty)" "$file"; then needs_prelude=1 fi done # Check if the prelude is needed and does not already exist; if so, add it if [ "$needs_prelude" = "1" ] && ! rg -q 'use crate::prelude::\*' "$file"; then # Split the file into two parts: module-level attributes and rest # Imports will be added after module-level attributes attrs=$(awk '/^#!|^\/\/!/ {found=NR} {lines[NR]=$0} END {for (i=1; i<=found; i++) print lines[i]}' "$file") rest=$(awk '/^#!|^\/\/!/ {found=NR} END {if (found) {for (i=found+1; i<=NR; i++) print lines[i]} else {for (i=1; i<=NR; i++) print lines[i]}} {lines[NR]=$0}' "$file") printf "%s\n" "$attrs" > "$file" printf "\n%s\n\n" "use crate::prelude::*;" >> "$file" printf "%s" "$rest" >> "$file" fi for ty in "${prelude[@]}"; do export TY="$ty" # env for perl to use # Remove simple imports `use crate::ty;` perl -pi -0777 -e 's/use ((crate|super)::)?($ENV{TY});//g' "$file" # Remove the type if it is part of a group import perl -pi -0777 -e 's/(use (crate|super)::\{?(.*|(\n.*){0,2}))\b$ENV{TY}\b,? ?/$1/g' "$file" # Replace pathed `crate::ty` perl -pi -0777 -e 's/(crate|super)::($ENV{TY})\b/$2/g' "$file" done # For some reason, rustfmt doesn't trim leading newlines. Do so manually here. perl -pi -0777 -e 's/\A\n+//' "$file" rustfmt "$file" done ./ci/style.sh
- Loading branch information
Showing
149 changed files
with
1,521 additions
and
1,531 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.