418418 if [[ -n " ${dependencies_words:- } " ]]; then
419419 echo $' \n ' " ${dependencies_words} " >> .github/.cspell/rust-dependencies.txt
420420 fi
421- check_diff .github/.cspell/rust-dependencies.txt
422- if ! grep -Eq " ^\.github/\.cspell/rust-dependencies.txt linguist-generated" .gitattributes; then
421+ if [[ -z " ${REMOVE_UNUSED_WORDS:- } " ]]; then
422+ check_diff .github/.cspell/rust-dependencies.txt
423+ fi
424+ if ! grep -Fq ' .github/.cspell/rust-dependencies.txt linguist-generated' .gitattributes; then
423425 error " you may want to mark .github/.cspell/rust-dependencies.txt linguist-generated"
424426 fi
425427
@@ -443,17 +445,31 @@ EOF
443445 done
444446
445447 # Make sure the project-specific dictionary does not contain unused words.
446- unused=' '
447- for word in $( grep -v ' //.*' " ${project_dictionary} " || true) ; do
448- if ! grep <<< " ${all_words}" -Eq -i " ^${word} $" ; then
449- unused+=" ${word} " $' \n '
448+ if [[ -n " ${REMOVE_UNUSED_WORDS:- } " ]]; then
449+ grep_args=()
450+ for word in $( grep -Ev ' ^//.*' " ${project_dictionary} " || true) ; do
451+ if ! grep -Eqi " ^${word} $" <<< " ${all_words}" ; then
452+ grep_args+=(-e " ^${word} $" )
453+ fi
454+ done
455+ if [[ ${# grep_args[@]} -gt 0 ]]; then
456+ info " removing unused words from ${project_dictionary} "
457+ res=$( grep -Ev " ${grep_args[@]} " " ${project_dictionary} " )
458+ printf ' %s\n' " ${res} " > | " ${project_dictionary} "
459+ fi
460+ else
461+ unused=' '
462+ for word in $( grep -Ev ' ^//.*' " ${project_dictionary} " || true) ; do
463+ if ! grep -Eqi " ^${word} $" <<< " ${all_words}" ; then
464+ unused+=" ${word} " $' \n '
465+ fi
466+ done
467+ if [[ -n " ${unused} " ]]; then
468+ error " unused words in dictionaries; please remove the following words from ${project_dictionary} or run ${0##*/ } with REMOVE_UNUSED_WORDS=1"
469+ printf ' =======================================\n'
470+ printf ' %s' " ${unused} "
471+ printf ' =======================================\n'
450472 fi
451- done
452- if [[ -n " ${unused} " ]]; then
453- error " unused words in dictionaries; please remove the following words from ${project_dictionary} "
454- echo " ======================================="
455- echo -n " ${unused} "
456- echo " ======================================="
457473 fi
458474 fi
459475fi
0 commit comments