-
Notifications
You must be signed in to change notification settings - Fork 152
Code contracts suggestions for DataGridView #386
Description
(This isn't an exhaustive list of contracts worth adding, just what I've noticed so far)
- CreateColumnsInstance() and CreateRowsInstance() should all ensure non-null result.
That way, Rows and Columns can ensure non-null as well.
The main reason this is a problem is due to Windows Form Designer generated code in InitializeComponent(). It can make lines like these
this.DataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
which CC static analyzer will think is possibly accessing a null reference.
- DataGridViewRowCollection should require for all methods that take in a DataGridViewRow that the row is non-null, and Ensure for all methods that return a DataGridViewRow that the result is non-null
This seems pretty safe to me after going through the code, exception is thrown when null is passed in in relevant places already. But there is one tricky edge case that needs to be cleared up: DataGridViewBand.Clone() needs to guarantee it returns non-null.
public virtual object Clone()
{
DataGridViewBand dataGridViewBand = (DataGridViewBand) Activator.CreateInstance(base.GetType());
if (dataGridViewBand != null)
{
this.CloneInternal(dataGridViewBand);
}
return dataGridViewBand;
}
It's probably safe, but maybe there's some edge case where Activator.CreateInstance returns null? It calls out to external methods so I can't fully analyze it.
- DataGridViewRow CreateCellsInstance() and Cells should Ensure non-null
Similar logic as 1).
- DataGridViewColumnCollection should require non-null columns to be added, and ensure columns retrieved are non-null
Similar logic as 2), haven't analyzed yet though.
- Control.DataBindings can't be null, ButtonBase.FlatAppearance can't be null, ImageList.Images can't be null
Checked and it's not possible, so just add contracts.