From 366577f2946ed555451418ae7383e9c6fbb29c44 Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Thu, 12 Jan 2023 17:50:44 -0500 Subject: [PATCH] Add Mail.require_silently for parser warnings Previously, requiring parsers emitted a number of unused variable and unreachable statement warnings. The unused variable warnings are simple to fix (see skipkayhil/mail@8dd1655dc4628e34d9d654a1526bac7cd9513252), but the unreachable statements do not seem to be. Since it seems like its difficult to make Ragel generated parsers not emit any warnings, this commit adds a helper method that wraps require and silences the warnings emitted. The changes to $VERBOSE in the parsers added in 7ef5694b77bbe86be851dbcfae38b7b47c689555 unfortunately do not help as the warnings are emitted on parse and not when evaluating the files. --- lib/mail.rb | 7 ++++++ lib/mail/elements/address.rb | 2 +- lib/mail/elements/address_list.rb | 2 +- .../elements/content_disposition_element.rb | 2 +- lib/mail/elements/content_location_element.rb | 2 +- .../content_transfer_encoding_element.rb | 2 +- lib/mail/elements/content_type_element.rb | 2 +- lib/mail/elements/date_time_element.rb | 2 +- lib/mail/elements/envelope_from_element.rb | 2 +- lib/mail/elements/message_ids_element.rb | 2 +- lib/mail/elements/mime_version_element.rb | 2 +- lib/mail/elements/phrase_list.rb | 2 +- lib/mail/elements/received_element.rb | 2 +- lib/mail/parsers.rb | 22 +++++++++---------- 14 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/mail.rb b/lib/mail.rb index 1d22580a0..c333606b7 100644 --- a/lib/mail.rb +++ b/lib/mail.rb @@ -36,6 +36,13 @@ def self.eager_autoload! @@autoloads.each { |_,path| require(path) } end + def self.require_silently(file) # :nodoc: + before, $-w = $-w, false + require(file) + ensure + $-w = before + end + # Autoload mail send and receive classes. require 'mail/network' diff --git a/lib/mail/elements/address.rb b/lib/mail/elements/address.rb index edf7235eb..ac5d8fdbb 100644 --- a/lib/mail/elements/address.rb +++ b/lib/mail/elements/address.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/address_lists_parser' +Mail.require_silently 'mail/parsers/address_lists_parser' require 'mail/constants' require 'mail/utilities' diff --git a/lib/mail/elements/address_list.rb b/lib/mail/elements/address_list.rb index 49984d562..dc6f22708 100644 --- a/lib/mail/elements/address_list.rb +++ b/lib/mail/elements/address_list.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/address_lists_parser' +Mail.require_silently 'mail/parsers/address_lists_parser' module Mail class AddressList #:nodoc: diff --git a/lib/mail/elements/content_disposition_element.rb b/lib/mail/elements/content_disposition_element.rb index dfc6673d3..d7c558677 100644 --- a/lib/mail/elements/content_disposition_element.rb +++ b/lib/mail/elements/content_disposition_element.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/content_disposition_parser' +Mail.require_silently 'mail/parsers/content_disposition_parser' module Mail class ContentDispositionElement #:nodoc: diff --git a/lib/mail/elements/content_location_element.rb b/lib/mail/elements/content_location_element.rb index 7e1b22feb..4553ba522 100644 --- a/lib/mail/elements/content_location_element.rb +++ b/lib/mail/elements/content_location_element.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/content_location_parser' +Mail.require_silently 'mail/parsers/content_location_parser' module Mail class ContentLocationElement #:nodoc: diff --git a/lib/mail/elements/content_transfer_encoding_element.rb b/lib/mail/elements/content_transfer_encoding_element.rb index 53cd4ada3..68e009bfb 100644 --- a/lib/mail/elements/content_transfer_encoding_element.rb +++ b/lib/mail/elements/content_transfer_encoding_element.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/content_transfer_encoding_parser' +Mail.require_silently 'mail/parsers/content_transfer_encoding_parser' module Mail class ContentTransferEncodingElement #:nodoc: diff --git a/lib/mail/elements/content_type_element.rb b/lib/mail/elements/content_type_element.rb index b8086f429..549b378d3 100644 --- a/lib/mail/elements/content_type_element.rb +++ b/lib/mail/elements/content_type_element.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/content_type_parser' +Mail.require_silently 'mail/parsers/content_type_parser' module Mail class ContentTypeElement #:nodoc: diff --git a/lib/mail/elements/date_time_element.rb b/lib/mail/elements/date_time_element.rb index cc58fb998..373371502 100644 --- a/lib/mail/elements/date_time_element.rb +++ b/lib/mail/elements/date_time_element.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/date_time_parser' +Mail.require_silently 'mail/parsers/date_time_parser' module Mail class DateTimeElement #:nodoc: diff --git a/lib/mail/elements/envelope_from_element.rb b/lib/mail/elements/envelope_from_element.rb index 49c3743d1..3b669e917 100644 --- a/lib/mail/elements/envelope_from_element.rb +++ b/lib/mail/elements/envelope_from_element.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/envelope_from_parser' +Mail.require_silently 'mail/parsers/envelope_from_parser' require 'date' module Mail diff --git a/lib/mail/elements/message_ids_element.rb b/lib/mail/elements/message_ids_element.rb index bdf3cebec..dab7519c1 100644 --- a/lib/mail/elements/message_ids_element.rb +++ b/lib/mail/elements/message_ids_element.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/message_ids_parser' +Mail.require_silently 'mail/parsers/message_ids_parser' require 'mail/utilities' module Mail diff --git a/lib/mail/elements/mime_version_element.rb b/lib/mail/elements/mime_version_element.rb index a065a604a..c44e706c7 100644 --- a/lib/mail/elements/mime_version_element.rb +++ b/lib/mail/elements/mime_version_element.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/mime_version_parser' +Mail.require_silently 'mail/parsers/mime_version_parser' module Mail class MimeVersionElement #:nodoc: diff --git a/lib/mail/elements/phrase_list.rb b/lib/mail/elements/phrase_list.rb index f29edafaa..e9e61ec23 100644 --- a/lib/mail/elements/phrase_list.rb +++ b/lib/mail/elements/phrase_list.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/phrase_lists_parser' +Mail.require_silently 'mail/parsers/phrase_lists_parser' require 'mail/utilities' module Mail diff --git a/lib/mail/elements/received_element.rb b/lib/mail/elements/received_element.rb index b9198ec64..20fea846c 100644 --- a/lib/mail/elements/received_element.rb +++ b/lib/mail/elements/received_element.rb @@ -1,6 +1,6 @@ # encoding: utf-8 # frozen_string_literal: true -require 'mail/parsers/received_parser' +Mail.require_silently 'mail/parsers/received_parser' require 'mail/utilities' require 'date' diff --git a/lib/mail/parsers.rb b/lib/mail/parsers.rb index 0ea8ea5ac..8bf919518 100644 --- a/lib/mail/parsers.rb +++ b/lib/mail/parsers.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true -require 'mail/parsers/address_lists_parser' -require 'mail/parsers/content_disposition_parser' -require 'mail/parsers/content_location_parser' -require 'mail/parsers/content_transfer_encoding_parser' -require 'mail/parsers/content_type_parser' -require 'mail/parsers/date_time_parser' -require 'mail/parsers/envelope_from_parser' -require 'mail/parsers/message_ids_parser' -require 'mail/parsers/mime_version_parser' -require 'mail/parsers/phrase_lists_parser' -require 'mail/parsers/received_parser' +Mail.require_silently 'mail/parsers/address_lists_parser' +Mail.require_silently 'mail/parsers/content_disposition_parser' +Mail.require_silently 'mail/parsers/content_location_parser' +Mail.require_silently 'mail/parsers/content_transfer_encoding_parser' +Mail.require_silently 'mail/parsers/content_type_parser' +Mail.require_silently 'mail/parsers/date_time_parser' +Mail.require_silently 'mail/parsers/envelope_from_parser' +Mail.require_silently 'mail/parsers/message_ids_parser' +Mail.require_silently 'mail/parsers/mime_version_parser' +Mail.require_silently 'mail/parsers/phrase_lists_parser' +Mail.require_silently 'mail/parsers/received_parser'